[英]React how re-mount component after route change
i'm using react-redux in my project. 我在我的项目中使用react-redux。
I have some component where i'm dispatch some action and get data from reducer into a component on componentWillMount method here is problem when i change to route componentWillMount method not firing my actions or anything else. 我有一些组件,我在其中调度一些操作,并将来自减速器的数据获取到componentWillMount方法上的组件中,当我更改为路由componentWillMount方法而不触发我的动作或其他操作时,这就是问题。 for that i need to refresh page.
为此,我需要刷新页面。
here is my componentWillMount Method: 这是我的componentWillMount方法:
componentWillMount() {
console.log("d")
this
.props
.getChartData()
data = this.props.chartData
}
and here is my main.js where i import my components and assign routing. 这是我的main.js ,我在其中导入组件并分配路由。
import CompA from 'components/CompA'
import CompB from 'components/CompB'
const App = () => (
<Container fluid>
<div>
<main>
<Switch>
<Route exact path="/page-a" component={ComponentA} />
<Route exact path="/page-b" component={ComponentB} />
</Switch>
</main>
</div>
</Container>
)
export default App
where i mistake or what i need to do with it? 我在哪里出错或需要怎么做?
and its index.js: 及其index.js:
const target = document.querySelector('#root')
console.log(store.getState())
render(
<Provider store={store}>
<ConnectedRouter history={history}>
<div className="row">
<App />
</div>
</ConnectedRouter>
</Provider>,
target
)
如果您使用redux,则应使用mapStateToProps更改组件中的状态
I think you are using ConnectedRouter from the package react-router-redux, which is now deprecated. 我认为您正在使用来自软件包react-router-redux的ConnectedRouter,现已弃用。
For react-router 4, You need to use withRouter react-router-dom to wrap the component before connecting it like this: withRouter(connect(...)(MyComponent))
You can get also get the match, location, history properties via props once the component is wrapped in withRouter. 对于react-router 4,您需要使用withRouter react-router-dom来包装组件,然后再像这样进行连接:
withRouter(connect(...)(MyComponent))
您还可以获得匹配,位置,历史记录属性一旦组件被withRouter包装,就可以通过props。
For further information on withRouter, you can visit the following link: https://reacttraining.com/react-router/web/api/withRouter 有关withRouter的更多信息,您可以访问以下链接: https ://reacttraining.com/react-router/web/api/withRouter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.