I am following a tutorial and copied everything but got this error
TypeError: event.preventDefault is not a function
in the onSubmit() but the event.preventDefault works in the onChange()
Here is the code i have:
import React, { Component } from 'react'
import '../App.css';
import ipfs from './ipfs';
class Add extends Component {
constructor(props) {
super(props);
this.state = {
ipfsHash: '',
buffer: null,
};
this.captureFile = this.captureFile.bind(this);
this.onSubmit = this.onSubmit(this);
}
captureFile(event) {
event.preventDefault();
const file = event.target.files[0];
const reader = new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = () => {
this.setState({ buffer: Buffer(reader.result) });
console.log('buffer', this.state.buffer);
};
}
onSubmit(event) {
event.preventDefault();
ipfs.files.add(this.state.buffer, (error, result) => {
if (error) {
console.error(error);
return;
}
this.setState({ ipfsHash: result[0].hash });
console.log('ipfsHash', this.state.ipfsHash);
});
}
render() {
return (
<div>
<h1>Add a Graduate</h1>
<form onSubmit={this.onSubmit}>
<input type='file' onChange={this.captureFile} />
<input type='submit' />
</form>
<h1>Transcript of Record</h1>
<img src={'https://ipfs.io/ipfs${this.state.ipfsHash}'} alt=''></img>
</div>
);
}
}
export default Add;
I don't understand what is the error all about.