简体   繁体   English

反应如何在 redux-saga 中导航路由器?

[英]react how to navigate router in redux-saga?

version: "react-router-dom": "^4.1.2", "react-router-redux": "^5.0.0-alpha.8",版本:“react-router-dom”:“^4.1.2”,“react-router-redux”:“^5.0.0-alpha.8”,

i can navigate router successfully in my component by this way:我可以通过这种方式在我的组件中成功导航路由器:

this.props.history.push('/cart')

then i wanna navigate router in my saga.js , i tried some ways and they all not worked:然后我想在我的 saga.js 中导航路由器,我尝试了一些方法,但它们都不起作用:

const history = createHistory();



yield call(history.push, '/cart')
yield call(put('/cart)//react-router-redux
history.push('/cart')

these ways can change the url , but the page would not render.这些方法可以更改 url ,但页面不会呈现。 i add the page component withRouter , and it also not work.我用Router添加了页面组件,它也不起作用。 can someone help me thanks!有人可以帮我吗谢谢!

here is my some settings:这是我的一些设置:

const render = Component =>
  ReactDOM.render(
      <Provider store={store}>
        <AppContainer>
          <Component />
        </AppContainer>
      </Provider>
    ,
    document.getElementById('root')
  );

class App extends Component {

  render () {
    return (
      <ConnectedRouter history={history}>
        <div>
          <Header/>
          <Nav/>
          <ScrollToTop>
            <Route render={({location}) => (
              <ReactCSSTransitionGroup
                transitionName="fade"
                transitionEnterTimeout={300}
                transitionLeaveTimeout={300}>
                <div key={location.pathname} className="body">
                  <Route location={location} exact path="/" component={HomePageContainer}/>
                  <the other component>
                  .....
                </div>
              </ReactCSSTransitionGroup>)}/>
          </ScrollToTop>
          <Footer/>
        </div>
      </ConnectedRouter>
    )
  }
}

=============================================================== ==================================================== ==============

i fixed this problem by this way: user Router instead of BroswerRouter,and then我通过这种方式解决了这个问题:用户路由器而不是 BroswerRouter,然后

 history.push('/somerouter')

You can use push method from react-router-redux . 您可以使用react-router-redux push方法。 For example: 例如:

import { push } from "react-router-redux";

yield put(push('/')); /* inside saga generator function*/

You can use browserHistory of react-router . 你可以使用的是react-router browserHistory

import { browserHistory } from "react-router";

yield browserHistory.push("/"); //put the path of the page you want to navigate to instead of "/"

I've found a simple solution. 我找到了一个简单的解决方案。 Dispatch the push function that is inside the component to the saga. 将组件内部的推送功能发送到saga。

class App extends Component {
    foo = () => {
        const { dispatch } = this.props;

        dispatch({
            type: 'ADD_USER_REQUEST',
            push: this.props.history.push <<-- HERE
        });
    }

    render() { ... }
}

Inside the saga, just use the push like this: 在传奇中,只需使用这样的推送:

function* addUser(action) {
    try {
        yield put(action.push('/users'));

    } catch (e) {
        // code here...
    }
}

Had to resolve similar issue recently.最近不得不解决类似的问题。 react-router-redux project is deprecated now, so I used connected-react-router - works well. react-router-redux 项目现在已弃用,所以我使用了 connected-react-router - 效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM