javascript - 尝试使用 componentWillReceiveProps 更改按钮颜色

标签 javascript html css reactjs redux

  • 我正在尝试使用 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>
        );
      }
    }

最佳答案

那是来自 SandBOXapp.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/

相关文章:

javascript - Bootstrap 4 工具提示未按样式显示。而且它仅适用于 anchor 标记

javascript - 将 AngularJS 与 RequireJS 集成

html - 在需要时缩小文本以适应 CSS 中的 div

html - 如何更改表格元素中的元素堆叠顺序?

javascript - 第二次插入后文本未出现

html - 带有静态列的响应式表格布局?

javascript - JS根据key值拼接数组值

html - 基于屏幕宽度的可变列数

html - 如何创建应用了背景图像的倾斜 div?

javascript - 如何测试 mongodb criteria 对象中的两个外部值是否相等?