[英]Can't access redux store after adding react routing. MapStateToProps is not being invoked
I'm learning redux.我正在学习 redux。 Wrote some simple components for fetching data from API.
编写了一些从 API 获取数据的简单组件。 Everything worked fine until I added routing.
一切正常,直到我添加了路由。
I'm wrapping my Router with Provider.我正在用 Provider 包装我的路由器。 I have route '/cars' and component VisibleCarList which accesses store using connect.
我有路由“/cars”和组件 VisibleCarList 使用连接访问商店。 When accessing '/cars' via Link or direct URL(tried both, same result) mapStateToProps is not invoked at all, can't see any redux props.
当通过链接或直接 URL 访问“/cars”(两者都试过,结果相同)时,根本没有调用 mapStateToProps,看不到任何 redux 道具。
However if I go to for example '/example' with routed App component which has VisibleCarList in it everything works fine.但是,如果我转到例如带有 VisibleCarList 的路由 App 组件的“/example”,一切正常。
Spent a few hours already and I still don't understand why it can't connect to the store.已经花了几个小时,我仍然不明白为什么它无法连接到商店。 Any ideas?
有任何想法吗?
index.js索引.js
const store = createStore( reducer, composeEnhancers(
applyMiddleware(thunkMiddlewares)))
ReactDOM.render(
<Provider store={store}>
<Router>
<Route path="/example" component={App}/>
<Route path="/cars" component={VisibleCarList}/>
</Router>
</Provider>
, document.getElementById('root'));
VisibleCarList可见汽车列表
export class VisibleCarList extends Component {
componentDidMount() {
this.props.getCarsPage(0);
}
render() {
...
}
}
const mapStateToProps = (state) => {
return{
cars: state.cars,
pagination: state.pagination,
}
}
const mapDispatchToProps = {
getCarsPage: fetchCarPage,
setPage: setCurrentPage,
}
export default connect(mapStateToProps, mapDispatchToProps)(VisibleCarList)
App.js应用程序.js
function App(props) {
return (
<Box bgcolor="primary.light">
<NavBar />
<VisibleCarList></VisibleCarList> //WORKS FINE
</Box>
);
}
export default App;
I think the import
statement may be wrong.我认为
import
语句可能是错误的。
You have to use import VisibleCarList from '...';
你必须使用
import VisibleCarList from '...';
rather than import { VisibleCarList } from '...';
而不是
import { VisibleCarList } from '...';
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.