gpt4 book ai didi

javascript - 使用 componentWillRecieveProps 从 App.js 接收 Prop

转载 作者:太空宇宙 更新时间:2023-11-04 00:43:57 25 4
gpt4 key购买 nike

  • 在我的按钮组件的 app.js 中,我给了这个 Prop propTwo={"two"}
  • 但 Button.js 仍然没有在 componentWillReceiveProps 中打印任何内容
  • 你能告诉我如何使用 componentWillReceiveProps 方法接收 Prop
  • 你能告诉我如何在我的应用程序中使用 componentWillReceiveProps
  • 在下面提供我的代码片段和沙箱

https://codesandbox.io/s/hopeful-villani-xyikq

class Button extends Component {
componentWillReceiveProps(nextprops) {
console.log("componentWillReceiveProps nextprops--->", nextprops);
}
render() {
return (
<div>
<button
onClick={() => {
// getPosts(channel);
// getAlert();
}}
className="btn btn-primary btn-lg btn-block"
>
Get top news
</button>
</div>
);
}
}

App.js

const App = () => (
<div>
<RecentChannelItem />
<ChannelsField propOne={"one"} />
<Button propTwo={"two"} />
<TopNews />
</div>
);

最佳答案

componentWillReceiveProps 已被弃用。

您可以使用 componentDidUpdate .

您可以通过访问 componentDidUpdate 中的 this.props 来访问更新的 Prop 。

关于javascript - 使用 componentWillRecieveProps 从 App.js 接收 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57667102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com