반응형
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) }

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

 

반응형
도움이 되셨다면 공감 클릭 부탁드리며
출처만 남겨주시면 글 내용은 마음껏 퍼가셔도 좋습니다 :)