I am building a React web app which is split up in multiple components accessible via react-tabs:
import React from 'react';
import ReactDOM from 'react-dom';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import a from './components/a';
import b from './components/b';
const TabNavigator = () => (
<Tabs>
<TabList>
<Tab> A </Tab>
<Tab> B </Tab>
</TabList>
<TabPanel>
<a />
</TabPanel>
<TabPanel>
<b />
</TabPanel>
</Tabs>
);
ReactDOM.render(<TabNavigator />, document.getElementById('root'));
Every tab is his own component/sub-system which is rendered freshly when the tab is accessed. In every tab I am using the data of one JSON-file. This data is loaded into the state of each component like this:
constructor(props) {
super(props);
this.state = {
data: json
};
}
I am now changing the state in one of the components to trigger a re-render with the new data:
this.setState({
data: editedJson
});
So far so good but when I am now switching to the other tab/component this.state.data
has changed there as well - why did this happen? Is the state shared between the components?
EDIT: Here is an MVCE where you can change the state in B and it will also change in A: