My react web app has a page component. The page component does the async calls for the child components. Now I have to this on every page
export default class Page extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true
}
}
componentWillMount = async () => {
// ... do calls
this.setState({
loading: false
})
}
render () {
if (this.state.loading) {
return <Loading/>
} else {
return (
// Some other components that needs the information from the calls
)
}
}
}
Is there a way to have less boilerplate? I was looking at Higher Order Components with ReactJS. I thought maybe a component that would get a function of the calls needed to be made and a component that renders the function.
const loader = (calls) => (WrappedComponent) => {
return class Loader extends React.Component {
constructor (props) {
super(props);
this.state = {
loading: true
}
}
componentWillMount = () => {
// execute calls function
};
render () {
return this.state.loading ? <Loading/> : <WrappedComponent {... this.props }/>
}
}
};
export default loader;
But I haven't figured out a way to pass a calls function into the loader function.