简体   繁体   English

将数据传递到顶级React组件

[英]Passing data to top-level React component

ReactJS newb question. ReactJS newb问题。 I'm sure this is obvious, but I can't find the place in the doc. 我确定这很明显,但是我无法在文档中找到该位置。

I have a React component, which is rendered like so: 我有一个React组件,其呈现方式如下:

React.render(
  <MyComponent data="{myVar}" />,
  document.getElementById("element")
);

When myVar changes, it doesn't appear to automatically call the componentWillReceiveProps method on MyComponent, which I assumed it would. 当myVar更改时,它似乎并没有自动调用MyComponent上的componentWillReceiveProps方法,而我认为会那样。 That's fine, but how do explicitly tell the component to update? 很好,但是如何明确指示组件进行更新? I gather setState() is done internally within the component. 我收集setState()是在组件内部完成的。

Okay, found it: looks like setProps was added for this very purpose, eg 好的,找到它:看起来为此目的添加了setProps ,例如

var myComponent = React.render(
   <MyComponent data={myVar} />,
   document.getElementById("element")
);

// ... later on ...

myComponent.setProps({ data: 'newData' });

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

相关问题 在顶级反应组件上调用方法 - calling method on top-level react component 我可以使用哑组件作为我的顶级组件与React / Redux App吗? - Can I use a dumb component as my top-level component with React/Redux App? 从组件返回React元素时,顶层标签上是否只有一个React.createElement()被调用? - When a React element is returned from a component, is just one React.createElement() called on the top-level tag? React是重新渲染整个顶级组件还是仅对其中发生更改的单个组件进行渲染? - Does React re-render the entire top-level component or just the individual components within which change? 是否可以重新加载顶级React Native组件以刷新所有子组件? - Possible to reload top-level React Native component in order to refresh all children components? 如何始终获取React组件的顶级DOM节点? - How to always get React component's top-level DOM node? 意外的顶级异常(反应原生) - UNEXPECTED TOP-LEVEL EXCEPTION (react-native) 从顶级App类更改React上下文 - Changing the React context from the top-level App class meteor.startup.js中的React Router(流星顶层) - React Router in meteor.startup.js (meteor top-level) 在顶级组件中将道具传递给 state - Passing props to state in top level component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM