儿童不会更新父母的状态变更后

发布于 2025-02-13 03:22:53 字数 1810 浏览 1 评论 0原文

在PlayerContainer的第一次渲染之后,我的父部件中的MovieID发生了另一种变化,并不会引起PlayerContainer的果实,我不明白为什么。

父组件:

export default class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      renderPlayer: false,
      movieId: ''
    }
  }

  handleCallback = (num) => {
    this.setState({ movieId: num, renderPlayer: true }, () => {
      console.log('MovieID ' + this.state.movieId)
    })
  }

  render() {
    const { movieId } = this.state;
    return (
      <div className="App">
        {this.state.renderPlayer
          ? <PlayerContainer movieId={movieId} />
          : null}
        <Selection handleClick={this.handleCallback} />
      </div>
    )
  }
}

选择组件接收单击,并导致父状态变化,然后将其传递给PlayerContainer:

export default class PlayerContainer extends Component {

    constructor(props) {
        super(props)
        this.state = {
            movies: [],
            DataisLoaded: false
        }
    }

    componentDidMount() {
        fetch("https://a.url.com/movies/" + this.props.movieId + "/1/")
            .then((res) => res.json())
            .then((json) => {
                this.setState({
                    movies: json,
                    DataisLoaded: true
                });
            })
    }

render() {
        const { DataisLoaded, movies } = this.state;
        return (
            <div className='wum__playercontainer section__padding' id='player'>
                {!DataisLoaded
                    ? <h1>Please wait a second...</h1>
                    : <ReactPlayer
                        url={movies.link}
                        ...
                        ...
                    />}
            </div>
        )
    }
}

After the first render of PlayerContainer another change of movieId in my Parent component doesn't cause a rerender of PlayerContainer and I don't understand why.

Parent component:

export default class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      renderPlayer: false,
      movieId: ''
    }
  }

  handleCallback = (num) => {
    this.setState({ movieId: num, renderPlayer: true }, () => {
      console.log('MovieID ' + this.state.movieId)
    })
  }

  render() {
    const { movieId } = this.state;
    return (
      <div className="App">
        {this.state.renderPlayer
          ? <PlayerContainer movieId={movieId} />
          : null}
        <Selection handleClick={this.handleCallback} />
      </div>
    )
  }
}

The Selection component receives a click and causes the state change in the parent, which then is passed on to PlayerContainer:

export default class PlayerContainer extends Component {

    constructor(props) {
        super(props)
        this.state = {
            movies: [],
            DataisLoaded: false
        }
    }

    componentDidMount() {
        fetch("https://a.url.com/movies/" + this.props.movieId + "/1/")
            .then((res) => res.json())
            .then((json) => {
                this.setState({
                    movies: json,
                    DataisLoaded: true
                });
            })
    }

render() {
        const { DataisLoaded, movies } = this.state;
        return (
            <div className='wum__playercontainer section__padding' id='player'>
                {!DataisLoaded
                    ? <h1>Please wait a second...</h1>
                    : <ReactPlayer
                        url={movies.link}
                        ...
                        ...
                    />}
            </div>
        )
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狂之美人 2025-02-20 03:22:53

这是因为您仅在componentDidmount中使用this.props.movi​​eid。如果您想再次渲染这样的组件,则必须实现componentDidupdate函数,以便每次渲染道具都会更改:

componentDidUpdate() {
// fetching again with new props
}

您甚至可以使用PrevProps param指定DIDUPDATE函数:

componentDidUpdate(prevProps) {
   if(prevProps.movieId !== this.props.movieId) {
      //fetch again your data
   }
}

This is because you only use this.props.movieId in the componentDidMount. If you want to render again your component like this you have to implement the componentDidUpdate function so it will render each time a props will change :

componentDidUpdate() {
// fetching again with new props
}

You could even specify the DidUpdate function using the prevProps param :

componentDidUpdate(prevProps) {
   if(prevProps.movieId !== this.props.movieId) {
      //fetch again your data
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文