I have a React Native application with the react-native-scrollable-tab-view library, where this tab view contains 3 child nodes:
App.js
<ScrollableTabView
style={{marginTop: 0, backgroundColor: '#1C4747', }}
tabBarUnderlineStyle={{backgroundColor: "#fff"}}
tabBarActiveTextColor={"#fff"}
tabBarInactiveTextColor={"#ccc"}
initialPage={0}
renderTabBar={() => <DefaultTabBar />}
>
<RotaEntrega tabLabel='ROTA' />
<ChildOne tabLabel='ORDENS' text='ORDENS' size={17} weight="500" />
<ChildTwo tabLabel='HISTÓRICO' text='HISTÓRICO' size={17} weight="500" />
</ScrollableTabView>
In RotaEntrega.js there is an HTTP request that returns a json from the server. However, I realized that this same request is used on the other two nodes (ChildOne and ChildTwo), so I would have to migrate the request to the App.js and use the result in the ScrollableTabView tabs. I can pass this data using props
, but the child tabs would have to modify the state in App.js ...
In summary:
App.js does HTTP request and gets JSON. Saves JSON in the state. Pass the state to the Rota Delay tabs, ChildOne, ChildTwo. The tabs read and modify the App.js state.
The request is currently RotaData.js:
_route = async(codDriver) => {
const apiCall = await fetch('http://urlaqui.com.br/testes/api.php?driver=${codDriver}');
const response = await apiCall.json();
let latLongObj = {
latitude: parseFloat(response.os[0].address.latitude),
longitude: parseFloat(response.os[0].address.longitude)
};
this.setState({
route: response.success ? [ response.os[0] ] : [], //only first route in the list
routeFound: response.success,
orders: response.success ? response.os : [], //and put the all orders to this array
destination: latLongObj
});
};
componentDidMount(){
this._route("51ds515");
}
Soon the above code will change to App.js.
How can I implement this?