Warning: Cannot update a component from inside the function body of a different component.

 

 

App.js
const [visible, setVisible] = useState(false);
<Modal>
<Component setVisible={setVisible}>
</Modal>

 

Component.js
export default (props) => {
return (
<>
<View>
<Button
onPress={props.setVisible(false)}>
/>
</View>
</>
)
}

 

부모에서 Modal 을 View 한 후에 (Component.js)

Button 을 클릭하면 props 으로 넘어온

부모의 state 를 setvisible(false) 로 modal  을 닫으려 했다

 

Warning: Cannot update a component from inside the function body of a different component.

 

Component.js
export default (props) => {
return (
<>
<View>
<Button
onPress={() => props.setVisible(false)}>
/>
</View>
</>
)
}

 

{ () => props.setVisible(false) }

 () => 을 통해 사용하면 된다

 



글이 도움이 되셨다면 공감과 광고 클릭 한번 부탁드립니다! 💕
감사합니다 ✨



댓글

댓글을 사용할 수 없습니다.