ReactJs React updating state for child components reactjs react native,reactjs react native share code,reactjs react

My app is structured as follows:

<App>
    <SelectItems/>
    <Tabs>
        <Tab> <Window1> </Tab>
        <Tab> <Window2> </Tab>
    </Tabs>
</App>

The App has a state element called allItems, and SelectItems is used to narrow this down to App.state.selectedItems. The tabs work in a way that only one of the tabs shows at a given time, it's a component from React-Bootstrap.

The Window1 and Window2 display data that depend on the selectedItems, the data (let's call it data1 and data2) is obtained via a computationally intensive api call. Here's the problem. The cleanest way to do this while thinking in React is to "pull the state up", so that data1 and data2 are updated from App whenever the selectedItems change, and the data is then stored in the state of App and passed down to the tabs. This however is inefficient, because only one Window is showing at a given time, so I'm wasting time updating data that I'm never showing. In my actual app I've got many tabs, so it's a real problem.

What's a way of fixing this? I want the Windows to update whenever the App.state.selection changes, and I looked at static getDerivedStateFromProps(props, state) but it looks like (but I'm not sure) that this function won't work because the state updating would be asynchronous, as it requires an API call.

Are there general strategies one can use in this situation?

Thanks!

Answer:1



  1. reactjs react native
  2. reactjs react native share code
  3. reactjs react router
  4. reactjs react table
  5. reactjs react hooks
  6. reactjs react router dom
  7. reactjs react fragment
  8. reactjs react-router-dom example

Iam trying to convert all selected item id's from a redux store into the item names and save it into state. My problem is that the spread operator inside the setState function is equal to zero. Do I ...

Iam trying to convert all selected item id's from a redux store into the item names and save it into state. My problem is that the spread operator inside the setState function is equal to zero. Do I ...

In a small React/Redux app that I am writing, I have a thunk that looks as follow: updateCategory(category){ return function(dispatch, getState){ dispatch({ type : types....

In a small React/Redux app that I am writing, I have a thunk that looks as follow: updateCategory(category){ return function(dispatch, getState){ dispatch({ type : types....