I'm using the same Form
component for editing a post and adding a new one in my application. The componentDidMount of Form
looks like this:
componentDidMount() {
this.props.match ? this.props.dispa({
type: 'ON_LOAD_ACTION',
payload: this.props.fetch.data.find(x => x.id == this.props.match.params.id)
}) : this.props.dispa({
type: 'ON_RESET',
payload: ''
});
}
It checks if there's an id
on the URL, e.g. localhost:3000/edit/id
I'm trying to update a post. I extract the data of the post and I can fill the form with it. If there's no id
on the URL I just render the form empty. The second dispatch is for resetting the state to its initial state.
I'm using react-router
/redux
and my problem is when I'm on the edit form and click on create form. It doesn't re-render and I got the value of the post I was in. I think that it's because i'm redirecting to the same form.
<Route exact path="/create" >
<FromProduct />
</Route>
<Route exact path="/edit/:id" component={FromProduct}></Route>
But if I click on submit
button to update the post or I go to the /home
first, the component re-renders perfectly.