- 我需要通过
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/