小编Har*_*h S的帖子

ReactJS 如何在 app.js 文件中使用 Router 的 props

我的项目的 app.js 文件如下。对于我过去常常this.props.history.push('/')重定向到特定路径的所有其他组件,它会在这些组件接收props. 但在这个app.js我试图安慰里面的道具价值 constructor componentWillMountrender但都给出了空数组。有什么办法可以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)

reactjs react-router-v4

4
推荐指数
1
解决办法
1676
查看次数

如何在获取 API 调用结果中使用“this”关键字

我正在使用以下获取 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)

javascript reactjs fetch-api react-redux

4
推荐指数
1
解决办法
764
查看次数

将数组值附加到React JS中的当前数组中

我有州的价值

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不删除数组当前值的情况下添加类似于状态的数组.我只想将新的值数组附加到当前状态数组.有人可以帮忙吗?

reactjs

0
推荐指数
2
解决办法
5285
查看次数