简体   繁体   English

如何在已安装的ReactJS组件中自动传递更新的道具

[英]How do I automatically pass updated props in mounted ReactJS component

I have a scenario where in my ReactJS component is mounted on jQuery attach point as below. 我有一个场景,我的ReactJS组件安装在jQuery附加点,如下所示。

In below code CustomfieldsDrawer is a react component and props is the object that I am passing as props. 在下面的代码中,CustomfieldsDrawer是一个反应组件,props是我作为道具传递的对象。 $('#app') is the attach point where my React component will mount. $('#app')是我的React组件将挂载的附加点。

ReactDOM.render(React.createElement(CustomfieldsDrawer, props), $('#app'));

Now the props object is object that actaually gets updated by ajax call on some customer events, the problem that I am facing is the React component doesn't know when the props get changed and it doesn't automatically rerenders itself. 现在道具对象是通过ajax调用对某些客户事件进行actaually更新的对象,我面临的问题是React组件不知道道具何时被更改并且它不会自动重新呈现。 I had to call above line of code again whenever the props is actually changing. 每当道具实际改变时,我不得不再次调用上面的代码行。 Do you guys know a way that can facilitate the auto update for my React component? 你们知道一种方法可以促进我的React组件的自动更新吗?

Since props is a plain javascript object which was modified outside of React , React doesn't know that it was changed unless you tell it explicitly, in this case by re-rendering the component. 由于props是一个普通的javascript对象,它在React之外进行了修改,因此除非你明确地告诉它,否则React不会知道它已被更改,在这种情况下通过重新渲染组件。

So you can either wrap the code into some parent react component and call the ajax from it and then change it's state to contain the new data which would get passed to the child component and that would trigger automatic re-render. 所以,你可以换行代码到一些家长反应成分,并从中调用AJAX,然后改变它的状态含有会得到传递给子组件和将触发自动重新绘制新的数据。

Or , if you insist on having the code outside of React, you will have to re-render manually. 或者 ,如果您坚持在React之外使用代码,则必须手动重新渲染。 You can however create some handy managing code for this, for inspiration check out ReactDOM.render and the Top Level React API blog post on official React's site. 但是,您可以为此创建一些方便的管理代码,以获取灵感,查看ReactDOM.render和官方React网站上的顶级React API博客文章。

Also on side note, I am not sure how it's possible that the code you posted works. 另外在旁注,我不确定你发布的代码是如何工作的。 ReactDOM.render function expects the second parameter to be DOM object, but $('#app') returns jQuery object. ReactDOM.render函数期望第二个参数是DOM对象,但$('#app')返回jQuery对象。 Did you mean to have $('#app')[0] there instead? 你的意思是在那里有$('#app')[0]吗?

React component will update itself when we setState for it, so either in the ajax callback, we can setState here, or I think you may use componentWillReceiveProps() for your CustomfieldsDrawer component: 当我们为它设置setState时,React组件会自动更新,所以在ajax回调中,我们可以在这里设置setState,或者我认为你可以对CustomfieldsDrawer组件使用componentWillReceiveProps()

  componentWillReceiveProps(nextProps) {
    this.setState({
      //set you new state here, then in the render method, you should render using this state, instead of props
      someState: nextProps.yourNewProp,
    });
  }

  render() {
    // Use "{this.state.someState}" instead of "{this.props.yourProp}"
    return (
      <div>{this.state.someState}</div>
    );
  }

If this doesn't work yet, please post here your ajax function, or more code, so that I can understand more about your case, then we can fix it together, thanks 如果这还不行,请在这里发布你的ajax函数或更多代码,以便我能更好地了解你的情况,然后我们可以一起修复它,谢谢

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

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