State of child when parent calls useState update function - reactjs

Suppose I have the following code snippet (Please consider it as a pseudo code)
Parent.js
const [state,action]=useState(0);
return <View><Child1/><Button onPress={()=>action(1)}/></View>
Child1.js
const [state]=useState(Math.random());
return <Text>{state}</Text>
So my question is when I click the button in the parent will the Chil1 state change or not.
On my local machine it seems it changes.

The benefit of useState is that once a component is mounted, the state value does not change across re-renders until the update state function is called, passing a new state value.
Therefore, even though your parent component Button press state change triggers a rerender of the child, since the child component is only being rerendered and not unmounted/remounted, the initial state of Math.random() would remain the same.
See useState in React: A complete guide

I don't know what exact scenario is, but if you just set default state, the state will be memorized like Scenario 1
Scenario 1
In this way, the state of Child will not be changed even if Parent re-render
const Child = () => {
const [state] = useState(Math.random());
return <div>{state}</div>
}
const Parent = () => {
const [, action] = useState(true);
return (
<>
<button onClick={() => action(false)}>Not Change</button>
<Child />
</>
);
}
Scenario 2
Unless you remove it and then re-render Parent even if memorize all Child, that is
const Child = () => {
const [state] = useState(Math.random());
return <div>{state}</div>
}
const Parent = () => {
const [state, action] = useState(true);
useEffect(() => {
if (!state) action(true)
}, [state])
return (
<>
<button onClick={() => action(false)}>Change</button>
{state && <Child />}
</>
);
}
Scenario 3
By the may, if you don't use default state, in this way, it will be changed every rendering like that
const Child = () => {
return <div>{Math.random()}</div>
}
const Parent = () => {
const [, action] = useState(true);
return (
<>
<button onClick={() => action(prev => !prev)}>Change</button>
<Child />
</>
);
}
Scenario 4
If we don't want Child to re-render, we can try memo to memorize it
const Child = memo(() => {
return <div>{Math.random()}</div>
})
Scenario 5
However, when Child has props, perhaps we should invole useCallback or useMemo to make sure the values or memory addresses of props are "fixed" like constant, so that Child won't re-render
(We don't have to use useCallback or useMemo all the time, it doesn't much matter when there is no performance problem)
const Child = memo((props) => {
return <div {...props}>{Math.random()}</div>
})
const Parent = () => {
const [, action] = useState(true);
const style = useMemo(() => ({}), [])
const onOK = useCallback(() => alert(1), [])
return (
<>
<button onClick={() => action(prev => !prev)}>Change</button>
<Child className="test" style={style} onClick={onOK} />
</>
);
}

Related

how to pass dynamique data from child to parent in react native

i want to pass the data of text-input from child to parent to submit the dynamic form. when i use useEffect the phone blocked but i don't know why.please can someone help me to solve this problem.thanks to tell me if there are another way to pass the data.
child component
const RenderComponents = ({ sendChildToParent) => {
const [inputsVal, setInputsVal] = useState({});
const handleChange = (name, value) => {
setInputsVal({ ...inputsVal, [name]: value });
};
const senddata = () => {
sendChildToParent(inputsVal);
};
useEffect(senddata);
return (
<>
{getData.length === 0 ? (
<Empty />
) : (
getData.map((item, index) => {
switch (item.type) {
case "TextInput":
return (
<>
<InputText
onChangeText={(text) => handleChange(item.nameC, text)}
ModuleName={item.nameC}
placeholder={item.options.placeholder}
required={item.options.required}
key={index}
/>
</>
);
case "Phone":...
Parent Component
export function TemplateScreen(props) {
const navigation = useNavigation();
const [getData, setData] = React.useState(Mydata);
const [childData, setChildData] = useState([]);
const sendChildToParent = (dataFromChild) => {
setChildData(dataFromChild);
};
//*************************************Child Componenet*************** */
const RenderComponents = () => {
const [userTeam, setUserTeam] = useState({});
[...other code here...];
**********Parent Component*******
return (
<ScrollView>
<RenderComponents />
<Button
title="Submit"
onPress={()=>null}
/>...
The structure of your parent component is fine. The issues are in your child component, in the following lines:
const RenderComponents = ({ sendChildToParent) => {
const [inputsVal, setInputsVal] = useState({});
const handleChange = (name, value) => {
setInputsVal({ ...inputsVal, [name]: value });
};
const senddata = () => {
sendChildToParent(inputsVal);
};
useEffect(senddata);
it's not good practice to duplicate the input value in local state. Pass the value down from the parent component as well as the setter function.
you're not passing a dependency array to your useEffect function, so it runs on every render of the component. This sets off the following chain of events:
the parent renders
the child renders
useEffect runs, setting the value of the state in the parent
the parent re-renders
This is an endless loop and what causes your app to lock.
there's no need to wrap the state setting functions in your own functions unless you are planning to do additional work there later. There's also no need to run those functions in your component lifecycle (useEffect), because they will run when the input changes.
missing bracket in the first line.
You could rewrite the components in the following way:
// parent component
export function TemplateScreen(props) {
const navigation = useNavigation();
const [getData, setData] = React.useState(Mydata);
const [childData, setChildData] = useState({});
return (
<ScrollView>
<RenderComponents childData={childData} setChildData={setChildData} />
...
// child component
const RenderComponents = ({ childData, setChildData }) => {
const handleChange = (name, value) => {
setChildData({ ...childData, [name]: value });
};
return (
...

How to update child state when button in parent component is clicked

in child component I want to update the state when user clicked on button available in parent component and I've keep track of state value as this state is also affect by other code as well so I was thinking to use useEffect() hook but I'm not sure how to achieve it.
child component:
const [sentimentButtonValue, setSentimentButtonValue] = useState(false);
return(
<>
{sentimentButtonValue}
</>
)
parent Component:
const handelTableCardOpen = (idx) => {
// when this function call, want to update child 'sentimentButtonValue' state value
console.log(idx);
setSelectedRow(idx);
};
<Button key={idx} onClick={() => handelTableCardOpen (idx)}> Click </Button>
as others have stated, you need to life the state up, BUT JUST IN CASE you have a special case where you really need it
const Child = React.forwardRef((_, ref) => {
const [sentimentButtonValue, setSentimentButtonValue] = React.useState(false);
React.useImperativeHandle(ref, () => ({
whatEver: sentimentButtonValue,
setWhatEver: setSentimentButtonValue,
}));
return <>{sentimentButtonValue.toString()}</>;
});
const Parent = () => {
const childRef = React.useRef();
const handelTableCardOpen = () => {
childRef.current.setWhatEver(!childRef.current.whatEver);
};
return (
<>
<button onClick={handelTableCardOpen}>Click</button>
<Child ref={childRef} />
</>
);
};
You need to lift the state from the child to the parent component, and then pass that state as a prop to the child:
const Parent = () => {
const [sentimentButtonValue, setSentimentButtonValue] = useState(false)
const yourFunction = () => {
setSentimentButtonValue(newValue);
}
<Button
sentimentButtonValue={sentimentButtonValue}
onClick={yourFunction}
>
Click
</Button>
}

React useState update without set throught child component

I have a logic in my code that i don't understand, i can update a state (useState) throught my child components but without the "set" function.
Here is my (simplified) code :
const Main = () =>{
const [mission, setMission] = useState({activity:"", env:""})
const see = () =>{
console.log(mission)
}
return (
<Child1 data={mission} />
<button onClick={see}>TEST</button>
)
}
const Child1 = (props) =>{
const {data} = props
const [mission, setMission] = useState(data)
const handleChange = (value) =>{
mission["activity"] = value
}
return (
<Child2 data={mission} onChange={handleChange} />
)
}
const Child2 = (props) =>{
const {data} = props
const [activity, setActivity] = useState(data.activity)
const handleChange = (e) =>{
setActivity(e.target.value)
props.onChange(e.target.value)
}
return (
<input value={data} onChange={handleChange} />
)
}
I tried in sandbox and it work too, "mission" did update it's value throught all childs without any "setMission".
I'm relativily new to react so i miss something but i don't know what, can someone explain ?
Thank you
You probably want to "lift the state up". Only the Main component should useState. Then pass both the state value and update function to the child component. The child component will call this update function when it updates. This will update the state on the parent properly and re-render.
Using your example:
const Main = () => {
// Only one state at the top level
const [mission, setMission] = useState({ activity: "", env: "" });
const see = () => {
console.log(mission);
};
return (
<>
{/* Pass both the value and state update function to the child */}
<Child1 data={mission} update={setMission} />
<button onClick={see}>TEST</button>
<div>{mission.activity}</div>
</>
);
};
const Child1 = (props) => {
const { data, update } = props;
const handleChange = (e) => {
// This will set parent's state
update({ ...data, activity: e.target.value });
};
// You can follow the similar pattern to the next child
return <Child2 data={data} onChange={handleChange} />;
};
You can see a complete working example in this sandbox.

React parent pass children will trigger rerender without state/props change

When I have some children(HTML dom) as props to a child component and control in parent, I found that will trigger rerender!
Why React parent pass children will trigger rerender without state/props change?
How to avoid it? Check following.
const InsideChild = React.memo(({children}) => {
const countRef = useRef(0)
countRef.current += 1
return (
<div>render count: {countRef.current} {children}</div>
)
})
const OutsideParent = () => {
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
<div>
Test1:
<InsideChild />
</div>
<div>
Test2:
<InsideChild>
<p>children as html dom will not trigger rerender.</p>
</InsideChild>
</div>
</div>
)
}
as sample code, Test1 will not trigger rerender, Test2 will. Is that possible to avoid it?
More detail and working sample here:
https://codepen.io/sky790312/pen/QWqygxQ
The React.memo is not working when you are sending html is becuase you the children is an object in that case, the only way you can compare it is with a deep compare or something.
You can add a comparer function to your memo, and compare the inner props of the objects in the case you send an html.
In the example i just check if the props are an object and then compare the object inside, hope it helps you
const comparisonFn = (prevProps, nextProps) => {
if(typeof prevProps?.children === 'object') {
return prevProps?.children.props.children ==
nextProps?.children.props.children
} else {
return prevProps?.children === nextProps?.children;
}
}
const InsideChild = React.memo(({children}) => {
const countRef = useRef(0)
countRef.current += 1
return (
<div>render count: {countRef.current} {children}</div>
)
}, comparisonFn)

Why does child component re-render even if I have used useCallback?

const Root = () => {
/** useState */
const [count, setCount] = useState(1);
const [times, setTimes] = useState(1);
const handleCount = useCallback(() => {
setCount(count * 2);
}, []);
const handleTimes = useCallback(() => {
setTimes(times + 2);
}, []);
return (
<div>
<div>Root. counts: {count}</div>
<Counter onClick={handleCount} value={count} />
<Counter onClick={handleTimes} value={times} />
</div>
);
};
The child component will re-render when parent component re-renders, so I don't know why I should use useCallback. When will the function in useCallback change without my component re-rendering?
I suggest you to use React.memo in your child component.
This code will help you,
export const Counter = React.memo(props => {
return ...
})
Here is the doc link.
First, you haven't defined a dependency array for either of your useCallbacks. The child components would rerender if the function were rebuilt, or if the value were to change. Use should also be using the lazy set method of state.
const handleCount = useCallback(() => {
setCount(prev => prev * 2);
}, [setCount]);

Resources