简体   繁体   English

在React中单击子组件时如何呈现父组件?

[英]How to render a parent component upon clicking a child component in React?

I have three components each one being a parent of the other. 我有三个组成部分,每个组成部分都是另一个的父项。 I want to render the entire parent component upon clicking a child component. 我想在单击子组件时呈现整个父组件。 What is the correct way to achieve this? 实现此目的的正确方法是什么?

You can update state of parent component from child component. 您可以从子组件更新父组件的状态。 Parent will then re-render automatically. 家长随后会自动重新渲染。

Or 要么

You can pass forceUpdate method of parent component as a prop to child component. 您可以将父组件的forceUpdate方法作为对子组件的支持。 Whenever child component will call that method (eg on onClick handler), parent will re render. 每当子组件调用该方法(例如onClick处理程序)时,父组件将重新呈现。

Reference: 参考:

https://reactjs.org/docs/react-component.html#forceupdate https://reactjs.org/docs/react-component.html#forceupdate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM