Reloading components in react native

413 Views Asked by At

I'm creating a math quiz app using react-native. I wish to know how to reload all the components, upon clicking the right answer, so that a new question is loaded.

1

There are 1 best solutions below

1
On BEST ANSWER

You're looking the wrong way. Reloading all the components will just render the same thing. What you are looking for is more a thing like Redux.

It will allow you to have a state container where all your data live, allowing to store the question number and update it – then components will be rendered to display the new one.

Please take a look at redux documentation, then at react-redux one.

So you would create a dispatch method, e.g. setQuestion(...), which is called when you press a button that will change the question number. The button would be a presentational component.
Then, you would have a component that wrap the whole question screen that will be updated because it was bound with redux store. It is a container component.
See more about presentational and container component here.

If you still want to refresh your app, and don't want a predictive state, you could call app.forceUpdate() where app would be instance of the top component.