[react native] Warning: Cannot update a component from inside the function body of a different component.
반응형
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) }
() => 을 통해 사용하면 된다
반응형