I want to change the parent state from the child
JavaScript
x
17
17
1
export const ParentComponent=()=>{
2
3
const [ resendCodeStatus ,setResendCodeStatus ] = useState(false)
4
5
const callback=()=>{
6
7
setResendCodeStatus(!resendCodeStatus)
8
9
}
10
11
return (
12
13
< Timer callback={callback} />
14
15
)
16
}
17
but I can access the callback function just once
Advertisement
Answer
Try this:
JavaScript
1
10
10
1
export const ParentComponent=()=>{
2
3
const [ resendCodeStatus ,setResendCodeStatus ] = useState(false)
4
const callbackFunction=()=>{
5
setResendCodeStatus((status) => !status)
6
}
7
return (
8
<Timer callbackFunction={callbackFunction} />
9
) }
10