【问题标题】:history on redux state is not availb but it is available on react componentredux 状态的历史记录不可用,但在反应组件上可用
【发布时间】:2016-08-21 17:51:38
【问题描述】:

我的index.js 看起来像:

const store = configureStore()

render(
    <Root history={history} store={store} />,
  document.getElementById('root')
)

我的Root.js 看起来像:

class Root extends Component {
  render() {
    const { store } = this.props

    return (
      <Provider store={store}>
          <ReduxRouter />
      </Provider>
    )
  }
}

Root.propTypes = {
  store: PropTypes.object.isRequired,
  history: PropTypes.object
}

export default Root

而我的configureStore.js 是这样的:

export default function configureStore() {

  let createStoreWithMiddleware = compose(
    applyMiddleware(thunk),
    reduxReactRouter({ routes, createHistory })
    )(createStore)

  const store = createStoreWithMiddleware(rootReducer)

  return store

routes.js:

const routes =  (
    <Router>
        <Route path={"/"} component={LoginView}>
        <Route path={"/login"} component={DashboardView} />
        </Route>
  </Router>
)

export default routes

而我的LoginView 组件就像:

class LoginView extends Component {

    componentDidMount(){
        const {actions} = this.props
        actions.login()
    }

    render() {
        console.log("printing props")
        console.log(this.props)
        console.log(this.props.history)
        return (
            <div>
                <Login />
            </div>
        )
    }
}

export default LoginView


function mapStateToProps(state) {
    return {
        login: state.login
    }
}

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(Actions, dispatch)
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(LoginView)

我对@9​​87654331@ 感到很困惑。 在我的组件中,我在道具中得到history。就像我在组件中所做的 console.log(this.props.history) 一样 this.props.history

但在我的actions 中,我无法以任何方式获得history..

我的行为是这样的:

export function login(){

    return (dispatch, getState, history) => {
        console.log("get state")
        console.log(getState())
        console.log("history")
        console.log(history)
        var message = "hellooww world"
        return { message, type: loginTypes.LOGIN }
    }
}

在这里我可以得到getState(),但不能得到history

如果我想redirect 怎么办?我想要history,这样我就可以像这样重定向它:

history.pushState(null, '/dashboard')

谁能帮帮我..?真的很迷茫。。

【问题讨论】:

    标签: reactjs react-router redux react-redux react-router-redux


    【解决方案1】:

    我发现这个 GitHub 页面提供了一种从操作转换的简单方法,也许它会有所帮助(它有一个登录操作的示例):

    https://github.com/johanneslumpe/redux-history-transitions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2015-05-23
      相关资源
      最近更新 更多