0

我的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)

我很困惑history。在我的组件中,我得到history了道具。就像我在组件this.props.history中所做的那样。console.log(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')

谁能帮我.. ?真的很纠结这个。。

4

1 回答 1

0

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

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

于 2016-04-27T18:20:43.910 回答