【发布时间】:2019-12-29 18:15:36
【问题描述】:
- 我正在尝试使用 componentwillReceiveProps 来更改按钮颜色。
- 当我点击新闻频道时,获取头条新闻按钮应该会改变颜色。
- 所以我想在 Button.js 中我会使用 componentwillReceiveProps
- 这样在收到道具后我会更改按钮颜色。
- 但在 Button.js 的 componentwillReceiveProps 内没有打印任何内容。
- 我研究并找到了以下链接,但仍然没有帮助我 How do i use componentWillReceiveProps() correctly?
- 您能告诉我如何解决吗?
- 在下面提供我的代码 sn-p 和沙箱
https://codesandbox.io/s/boring-wu-btlre
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>
);
}
}
【问题讨论】:
-
Button的父级长什么样子? -
没有打印,因为您没有在
Button组件中传递任何道具 -
@Rikin 在我的代码 snadbox 中,你能像如何使用 componentWillReceiveProps 一样更新它吗...它太令人困惑了:(
-
只需将任何道具传递给您的按钮,您应该会很好。如果你不知道该怎么做,我建议你退后一步,学习 React 基础知识,以免以后头疼。对于您所追求的可能值得的方法已弃用
-
嘿,我尝试在 App.js 中添加道具,但仍然无法正常工作:(codeandbox.io/s/hopeful-villani-xyikq 你能在我的小提琴中更新吗,我坚持下去了
标签: javascript html css reactjs redux