I am using React useState to declare my object:
const [results, setResults] = useState();
console.log(results);
In same file I am returning React component and I am passing it some props:
<Table
data={results ? results.Companies : []}
setData={(newArr) => {
let newResults = results;
newResults.Companies = newArr.slice(0);
console.log(newResults);
return setResults(newResults);
}}
/>
P.S.: Keep in mind that I am passing to data only results.Companies which is array.
In this Table component I am changing the results.Companies property:
props.setData(arr);
So when it triggers function in Table component:
setData={(newArr) => {
let newResults = results; // I am assigning to newResults current result state
newResults.Companies = newArr.slice(0); // I am replacing newResults.Companies with new array I want to pass
console.log(newResults); // HERE is the desired results output
return setResults(newResults);
}}
The newResults variable is exactly the result I want to be passed to setResutlts. But this setting does not trigger re-rendering and this console.log(results) for example is not called. Why it is not trigger after I am passing new value to setResults()