- 我正在尝试使用 componentwillReceiveProps 来更改按钮颜色。
- 当我点击新闻 channel 时,获取热门新闻按钮应该改变颜色。
- 所以我想在 Button.js 中我会使用 componentwillReceiveProps
- 以便在我收到 Prop 后更改按钮颜色。
- 但在 Button.js 的 componentwillReceiveProps 内部没有任何打印。
- 我研究并找到了以下链接,但仍然没有帮助我 How do i use componentWillReceiveProps() correctly?
- 你能告诉我如何解决吗?
- 在下面提供我的代码片段和沙箱
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>
);
}
}
最佳答案
那是来自 SandBOX 的 app.js
,它清楚地表明您没有向按钮组件传递任何内容。这就是它不显示任何内容的原因。
import React from "react";
import ChannelsField from "./ChannelsField";
import RecentChannelItem from "./RecentChannelValues";
import Button from "../containers/Button";
import TopNews from "../containers/TopNews";
const App = () => (
<div>
<RecentChannelItem />
<ChannelsField />
<Button />
<TopNews />
</div>
);
export default App;
关于javascript - 尝试使用 componentWillReceiveProps 更改按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57642927/