儿童不会更新父母的状态变更后
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您仅在componentDidmount中使用this.props.movieid。如果您想再次渲染这样的组件,则必须实现componentDidupdate函数,以便每次渲染道具都会更改:
您甚至可以使用PrevProps param指定DIDUPDATE函数:
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 :
You could even specify the DidUpdate function using the prevProps param :