【问题标题】:api call for class component using componentWillRecievePropsapi 使用 componentWillRecieveProps 调用类组件
【发布时间】:2019-10-01 09:01:43
【问题描述】:
  • 我需要通过searchbar 组件将搜索值传递给menubar 组件以进行自定义搜索
  • 所以我从代码沙箱中取出一个现有的功能组件并转换为类组件。
  • 但我收到一个错误App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
  • 我正在尝试通过componentWillRecieveProps实现
  • 你能告诉我如何解决它吗?
  • 在下面提供我的代码 sn-p 和沙箱。

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。只需return (&lt;div&gt;testing&lt;/div);

标签: javascript html css reactjs redux


【解决方案1】:

只需将渲染函数更改为:

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

其余部分保持不变。如果您需要任何帮助或者这不起作用,请大声喊出来!

【讨论】:

    猜你喜欢
    • 2018-01-13
    • 2019-12-31
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    相关资源
    最近更新 更多