Using context to store an array and try to array.map to generate a list of component, but the UI did not re-render to show all the component.
Program background:
I have a context which contain a array and the UI will have a "Add" button.
My Goal:
- when user press "Add"
- array in context will be pushed a new value in it
- UI will be rerender and generate a new component
My problem:
I have try to use useState to store the array and that is completely OK since, once I update the list using setArray, that will triggle re-render and display a new component.
However, when I try to use useContext, the page will not be rerendered.
- Should I still use useState to perform my goal? (then I have to manage 2 varible which are the context and state, I think that is a dummy way to work on it.)
- is there any other method to achievel my goal?
Here is the sandbox demo: https://codesandbox.io/s/falling-field-ur748?file=/src/App2.js