I want to pass some data to the MainScreen component, but in the following example, loadData() is being called even when I access the /about route.
I could pass the loadData function and call it from MainScreen, but I don't want the component to have this responsibility.
How can I load the data only when the route is accessed?
class App extends React.Component {
loadData = () => {
console.log("Loading data");
return {
something: 123
};
}
render() {
return (
<Router>
<Switch>
<Route exact path="/">
<MainScreen data={this.loadData()} />
</Route>
<Router path="/about">
<About />
</Router>
</Switch>
</Router>
);
};
}