【问题标题】:How to update the props of a rendered react component from App.js?如何从 App.js 更新渲染的反应组件的道具?
【发布时间】:2018-05-16 13:16:41
【问题描述】:

我有一个 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})
    }

我已经在我的主 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>
    );
  }
}

我想在不重新创建组件的情况下更新我的 MoviesGallery 组件的道具。由于我已经将 componentWillReceiveProps() 添加到 MoviesGallery 组件,我该如何做到这一点,当单击“查询”按钮时,它会将新道具传递给 已经渲染 MoviesGallery 和 componentWillReceiveProps() 应该会导致它重新渲染,因为状态会改变。

只是对单击渲染的 MoviesGallery 组件时会更改道具本身的功能感到困惑。

提前致谢!

【问题讨论】:

  • 没有办法改变道具除非你用新的道具重新渲染它 - 你不能改变道具。当组件被重新渲染时,componentWillReceiveProps 被调用。
  • 如何在按钮单击时使用新道具重新渲染它?
  • 你没有发布MoviesGallery的渲染方法,但是当你只使用渲染内部的道具时,不需要设置本地状态。当一个组件收到一个新的 prop 时,它会用新的 prop 再次调用 render。
  • @HasanJafri 在你的父母那里做。接受MovieGallery 中的单击处理程序道具并将事件处理程序传递给它。然后,在事件处理程序中,在您的父级中设置状态,以便在单击按钮时设置父级的状态。该状态可以是您要传递给MovieGallery 的新道具。

标签: javascript reactjs


【解决方案1】:

当父组件向子组件传递一个新的(值)属性时,子组件会自动调用 render 方法。无需在子组件内设置本地状态来“存储”新道具。

这是一个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>

【讨论】:

    【解决方案2】:

    您可以为 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>
        );
      }
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2021-01-22
      • 2019-10-09
      • 2019-07-26
      相关资源
      最近更新 更多