REDUX RENDERING ERROR尝试用REDUX部分呈现代码时面临错误。已成功从文件中调度字符串,但未在app.js 上获取该字符串
import React from 'react';
import {Route, BrowserRouter} from 'react-router-dom'
import {connect} from 'react-redux'
import Home from './components/home'
import Data from './components/data'
import Nav from './components/nav'
import './App.css';
function App(props) {
const data = this.props.data //error in this line
return (
<div className="App">
<BrowserRouter>
<Nav />
<Route path='/' exact component={Home} />
<Route path='/data' component={Data} />
</BrowserRouter>
</div>
);
}
const mapStateToProps=(state)=>{
return {
data : state.data
}
}
export default connect(mapStateToProps)(App);
您使用的是功能组件,因此您必须在没有this
的情况下直接获得props
const data = props.data