I would like to pass data (which is saved as a state) to a react component that graphs that data. That graph should also be able to filter the data.
The data is a nested object structured as follows.
{
"mylog": {
"entries": [
{"Bool1": false, "Bool2": true, ...},
{"Bool1": true, "Bool2": true, ...},
...
]
},
"another_log": {...},
...
}
My approach has been to define a state called filteredData
within the graph component, set it to the data passed to the graph, and then update the filteredData
state when I want to filter the data.
function App(props) {
const [data, setData] = useState({...}); // Initial data here
return (
<div>
<Graph data={data} />
</div>
);
}
function Graph(props) {
const [filteredData, setFilteredData] = useState(props.data);
const filter = () => {
setFilteredData(data => {
...
});
}
return (
...
);
}
However, when filteredData
gets filtered, data
in the App component also gets filtered (and that breaks things). I've tried substituting {..props.data}
for props.data
in a couple of places, but that hasn't fixed it. Any ideas? Thanks in advance.
Here is a minimum, reproducible example: https://codesandbox.io/s/elastic-morse-lwt9m?file=/src/App.js