简体   繁体   English

更改路线后如何重新安装组件

[英]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.

相关问题 路线变化时反作用力重新安装组件 - React force re-mount component on route change 是否可以在React中重新安装组件? - Is it possible to re-mount a component in React? 使用 key prop 强制重新安装 React 组件 - Using the key prop to force re-mount of a React component 在重定向到具有不同特性的同一页面时,React Router触发触发器重新加载/重新安装组件 - React Router trigger re-load / re-mount component on redirecting to same page with different slug React-Router URL 在链接单击时导致组件重新安装(使用 useEffect(..)、[] 重新初始化) - React-Router URL Causes Component Re-Mount (Re-Initialization with useEffect(..), []) on Link Click React-子组件每次渲染时都会重新安装吗? - React - does a child component re-mount each time the parent component renders? React-router:更改查询字符串不会重新安装组件 - React-router: changing query string does not re-mount the component 什么可能导致 React Native 组件不断地卸载和重新安装? - What might be causing a React Native component to continuously un- and re-mount? 每次单击按钮时卸下并重新安装组件? - Dismount and re-mount a component on each button click? 如何更改组件中的路由确实已安装 - How to change the route in component did mount
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM