我的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')
谁能帮我.. ?真的很纠结这个。。