I would like to show an informational message when a user creates a task in my app. I have worked in react for a while now, but I am unable to think of the logic to show messages that disappear after showing once.
Here's the current setup
App.js Main component which uses react router dom to route to different pages and looks like:
class App extends Component {
constructor(props) {
super(props);
this.state = {
taskCreated: false,
};
}
showTaskCreatedAlert = () => {
this.setState({ taskCreated: true });
};
render() {
return (
<Router>
<div>
<Switch>
<Route
exact
path="/"
component={props => <TasksIndex {...this.state} />}
/>
<Route
path="/users/new"
component={props => (
<TasksNew
showTaskCreatedAlert={this.showUserCreatedAlert}
{...props}
/>
)}
/>
</Switch>
</div>
</Router>
);
}
}
TasksNew.js Component that renders a form where a user can create tasks
When a task is successfully created, I update state on my parent component (App.js and set taskCreated to true. Then I push the history to the root resource "/".
TasksIndex.js Component that renders all tasks created by the user This component gets the main components state passed to it as props and depending on whether the taskCreated is set to true or false, it will display that informational message
Everything is fine except that message does not go away after the user navigates away to /users/new and comes back. Only on a full reload does it disappear. Now I know why this is happening: The state of the parent component is never updated and taskCreated remains true.
But then how to accomplish this? How can I get the message to disappear once the user navigates to a different page in the app. I want to accomplish this without redux.