I'm attempting to memoize a Child component that contains a function that sets values to a parent array state and relies on the parent state. That, however, means that if I put the parent array state as a dependency, the number of rerenders do not make a significant improvement.
const Parent = () => {
const [state, setState] = useState([{},{},{}]);
const setA = (index, a) => {
let copyArr = [...state];
copyArr[index] = a;
setState(copyArr);
}
return (
state.map((item, index)=>{
<Child index={index} item={item} setA={setA}>
}
)
}
const Child = ({index, item, setA}) => {
return (
<View>
<Button onClick={() => {setA(index, Math.randInt(0,100))}>
<Text>{item.a}</Text>
</View>
)
}
So far what I've attempted was
const Parent = () => {
const [state, setState] = useState([{},{},{}]);
const setA = React.useCallBack((index, a) => {
let copyArr = [...state];
copyArr[index] = a;
setState(copyArr);
}, [state]);
return (
state.map((item, index)=>{
<Child index={index} item={item} setA={setA}>
}
)
}
const Child = React.memo(({index, item, setA}) => {
return (
<View>
<Button onClick={() => {setA(index, Math.randInt(0,100))}>
<Text>{item.a}</Text>
</View>
)
});
This however falls flat because whenever state as an array is updated, all sibling components rerender, due to the function being re-created.
Alternatively if I do not use state in the dependency array, the function does not re-create and the state internally doesn't change (Closure).
I'm wondering if there is a way to properly achieve this without significant revamp of the structure.