简体   繁体   English

如何从App.js更新呈现的React组件的道具?

[英]How to update the props of a rendered react component from App.js?

I have a React component MoviesGallery.js with the following configuration: 我有一个具有以下配置的React组件MoviesGallery.js:

class MoviesGallery extends Component {

    constructor(props) {
        super(props)
        this.state = { currentImage: 0 };
        this.closeLightbox = this.closeLightbox.bind(this);
        this.openLightbox = this.openLightbox.bind(this);
        this.gotoNext = this.gotoNext.bind(this);
        this.gotoPrevious = this.gotoPrevious.bind(this);
    }

    componentWillReceiveProps(nextProps) {
        this.setState({movies_genre: nextProps.movies_genre})
    }

I have rendered the component in my main App.js file like so: 我已经在主App.js文件中呈现了该组件,如下所示:

class App extends Component {

  render() {
    return (
      <MuiThemeProvider muiTheme={getMuiTheme(darkBaseTheme)}>
      <div className="App">
        <header className="App-header">
          <h1 className="App-title">Welcome to React</h1>
          <RaisedButton primary={true} label="Query" className="header_buttons"/>
          <RaisedButton secondary={true} label="Reset" className="header_buttons"/>
        </header>
        <MoviesGallery/>
      </div>
      </MuiThemeProvider>
    );
  }
}

I want to update the props of my MoviesGallery component without recreating the component. 我想更新MoviesGallery组件的道具,而无需重新创建该组件。 Since I already added the componentWillReceiveProps() to MoviesGallery component, how can I make it so when 'Query' button is clicked, it will pass new props to the already rendered MoviesGallery and componentWillReceiveProps() should cause it to re-render since the state will change. 由于我已经将componentWillReceiveProps()添加到MoviesGallery组件中,因此如何做到这一点,当单击“查询”按钮时,它将新道具传递给已经渲染的 MoviesGallery,并且componentWillReceiveProps()应该导致它重新渲染,因为状态为将改变。

Just confused about the function that will change the props themselves on-click of the rendered MoviesGallery component. 只是对将在渲染的MoviesGallery组件上单击时更改道具本身的功能感到困惑。

Thanks in advance! 提前致谢!

you can use the 'state' for your MovieGallery.js props because the state is an object that changes and you must your code like below : 您可以对MovieGallery.js props使用“状态”,因为state是一个会更改的对象,您必须像下面这样编写代码:

class App extends Component {
  state = {
    query : null
  }
  myFunction(query){
     this.setState({query});
  }
  render() {
    return (
      <MuiThemeProvider muiTheme={getMuiTheme(darkBaseTheme)}>
      <div className="App">
        <header className="App-header">
          <h1 className="App-title">Welcome to React</h1>
          <RaisedButton primary={true} label="Query" className="header_buttons" onClick={this.myFunction = this.myfunction.bind(this)}/>
          <RaisedButton secondary={true} label="Reset" className="header_buttons"/>
        </header>
        <MoviesGallery newProps = {this.state.query}/>
      </div>
      </MuiThemeProvider>
    );
  }
}

i hope it helps 我希望它会有所帮助

When a parent pass a new (value) prop to the child, the child component will call the render method automatically. 当父母将一个新的(值)道具传递给孩子时,孩子组件将自动调用render方法。 There is no need to set a local state inside the child component to "store" the new prop. 无需在子组件内部设置本地状态来“存储”新道具。

Here is a small example of a Counter that receives a count prop and just displays it, while the parent App in this case will change the value in its state and pass the new value to Counter : 这是一个Counter的小示例,该Counter接收一个count道具并仅显示它,而在这种情况下,父App将更改其状态的值并将新值传递给Counter

 class Counter extends React.Component { render() { const { count } = this.props; return ( <div>{count}</div> ); } } class App extends React.Component { constructor(props) { super(props); this.state = { count: 0 } } onClick = () => { this.setState({ count: this.state.count + 1 }); } render() { const { count } = this.state; return ( <div> <Counter count={count} /> <button onClick={this.onClick}>Add to counter</button> </div>); } } ReactDOM.render(<App />, document.getElementById('root')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

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

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