ctrlplusb / react-async-bootstrapper

Execute a bootstrap method on your React/Preact components. Useful for data prefetching and other activities.
MIT License
117 stars 12 forks source link

example with react-router-dom #12

Open CosticaPuntaru opened 6 years ago

CosticaPuntaru commented 6 years ago

As react router dom is the most used router for react. it will be nice to have an example of integrating the router with the bootstrapper to make sure the bootstrap function is called on route change (somehow)

CosticaPuntaru commented 6 years ago

a good enough solution is:


class DirectComponent extends Component {
    bootstrap() {
        console.log('direct bootstrap was called', this);
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(1)
            }, 2000)
        })
    }

    componentDidMount() {
        console.log('component did mount');
        this.bootstrap();
    }

    render() {
        console.log('render called');
        return (
            <div>
                my component
            </div>
        )
    }
}