当我这样做时,我在我的反应应用中收到此错误Uncaught (in promise) TypeError: Cannot read property 'hasChildNodes' of null
-
this.props.history.push('/search');
我正在使用react
16.2.0
和react-router-dom
4.2.2
我的路线定义如下 -
<Switch>
<Route exact path="/login" component={Login} />
<PrivateRoute exact path="/" component={HomePage} />
<PrivateRoute exact path="/authors" component={Authors} />
<PrivateRoute exact path="/books" component={Books} />
<PrivateRoute exact path="/bookDetail/:id" component={BookDetail} />
<PrivateRoute exact path="/editBook/:id" component={EditBook} />
<PrivateRoute exact path="/addBook" component={AddBook} />
<PrivateRoute exact path="/search" component={Search} />
</Switch>
当我在此处收到来自API的响应时,我的组件在我的句柄提交功能中出错 -
handleSubmit(event) {
event.preventDefault();
fetch(SEARCH_API_ENDPOINT+this.state.searchTerm, {
method: 'get',
headers: headers
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Something went wrong ...');
}
})
.then(data => {
console.log(data.docs);
this.props.history.push('/search');
//history.push('/authors') ;
//<Redirect to='/search'/>;
})
.catch(error => {
notify.show('Something is wrong with something!','error');
this.setState({ error:error, isLoading: false })
});
}
我不确定为什么会发生这种情况。如果您希望我粘贴更多详细信息,请与我们联系。
由于
答案 0 :(得分:0)
我会在发生以下情况时更新状态:
.then(data => {
console.log(data.docs);
this.setState(() => ({goodToRedirect: true}));
})
然后在render(),
中if(state.goodToRedirect) {
return (
<Redirect to="/authors"/>);
}