javascript - 使用 componentWillRecieveProps 调用类组件的 api

标签 javascript html css reactjs redux

  • 我需要通过 searchbar 组件将搜索值传递给 menubar 组件以执行自定义搜索
  • 所以我从代码沙箱中取出一个现有的功能组件并将其转换为类组件。
  • 但我收到一个错误 App(...): 没有从渲染中返回任何内容。这通常意味着缺少返回语句。或者,要不呈现任何内容,请返回 null。
  • 我试图通过componentWillRecieveProps实现
  • 你能告诉我如何解决吗?
  • 在下面提供我的代码片段和沙箱。

https://codesandbox.io/s/eloquent-galileo-14874

class App extends Component {
  state = {
    groupCheckBoxValues: [],
    groupRadioValue: "PRO"
  };

  componentWillReceiveProps({ search }) {
    console.log(search);
  }

  componentDidMount() {
    this.fetchdata("story");
  }

  fetchdata(type = "", search_tag = "") {
    var url = "https://hn.algolia.com/api/v1/search?tags=";
    fetch(`${url}${type}&query=${search_tag}`)
      .then(res => res.json())
      .then(data => {
        this.props.getData(data.hits);
      });
  }

  render() {
    <div> testing</div>;
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

最佳答案

只需将渲染函数更改为:

render() {
   return <div> testing</div>;
  }

其余的保持不变。如果您需要任何帮助或这不起作用,请大声喊叫!

关于javascript - 使用 componentWillRecieveProps 调用类组件的 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58159169/

相关文章:

javascript - jquery:ajax 加载内容全部加载完成时的事件(包括图像)

javascript - 如何检测元素是否在另一个元素之上

html - 在 WebKit 浏览器中滚动时背景颜色切换为透明

javascript - 使事件图标成为激活后的链接,如果未激活则不是链接

c# - 向 Accordion 添加箭头

javascript - 欧芹触发空字段错误

javascript - 加载脚本会动态阻塞渲染吗?

javascript - 如何使图像在网页上变得不可见且不可交互?

html - 在网页中嵌入 LaTeX 的最佳方式是什么?

html - 如何通过 CSS 设置水平滚动条的样式?