我的项目的 app.js 文件如下。对于我过去常常this.props.history.push('/')重定向到特定路径的所有其他组件,它会在这些组件接收props. 但在这个app.js我试图安慰里面的道具价值 constructor ,componentWillMount并render但都给出了空数组。有什么办法可以this.props.history.push('/')在 app.js 里面使用吗?
class App extends Component {
constructor(props) {
super(props);
this.state = {};
console.log(this.props)
}
componentWillMount(){
console.log(this.props)
this.props.history.push('/')
}
render() {
console.log(this.props)
return (
<Router>
<div className="App">
<Route exact path='/' render={(props) => <Login {...props} />} />
<Route exact path='/dashboard' render={(props) => <Dashboard {...props}/>}/>
</div>
</Router>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud) 我正在使用以下获取 API 来获取 API 结果。我需要在状态中设置结果数据,但“this”对象在回调函数中不可用。如何通过访问this关键字更新代码以设置状态中的值?
fetch(config.apiUrl + '/api/user', {
headers: authHeader(),
method: 'GET',
})
.then(function(res) {
return res.json();
})
.then(function(data) {
this.setState({ id: data.id });
});
Run Code Online (Sandbox Code Playgroud) 我有州的价值
this.state = {
emp: [
{id: "1", name: "A"}
{id: "2", name: "B"}
{id: "3", name: "B"}
]
}
Run Code Online (Sandbox Code Playgroud)
如何var arr = {id:"4", name:"D"}在emp不删除数组当前值的情况下添加类似于状态的数组.我只想将新的值数组附加到当前状态数组.有人可以帮忙吗?