【发布时间】: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)
我对@987654331@ 感到很困惑。
在我的组件中,我在道具中得到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