I would like to let users to move main component after they create content and I don't what methods I need to use. I wanted to use something like history but it didn't work. I am using
- "react": "^17.0.2",
- "react-dom": "^17.0.2",
- "react-router-dom": "^6.2.1",
import React, { Component } from 'react';
class CreateContent extends Component {
constructor(props) {
super(props);
this.state = {
content: {
title: ''
}
}
}
handleInput = (e) => {
this.setState({
content : {
...this.state.content,
[e.target.name]: e.target.value
}
})
}
addContent = async (e) => {
e.preventDefault();
console.log(JSON.stringify(this.state.title))
try {
const response = await fetch('http://localhost:9000/api/v1/content', {
method: 'POST',
body: JSON.stringify(this.state.content),
mode:'cors',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}
});
if(!response.ok) {
throw Error(response.statusText)
}
// ******* HERE I wanted to add something like *******
history.push('/main')
} catch (err) {
console.log('addContent failed -', err)
}
}
render() {
return (
<div>
<form onSubmit={this.addContent}>
<input
type="text"
name="title"
onChange={this.handleInput}
value={this.state.content.title}
/>
<input type="submit" value="submit" />
</form>
</div>
)
}
}
export default CreateContent