0
我index.js
樣子:歷史上Redux的狀態不availb但它可在反應成分
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')
誰能幫我...?真的很困惑這個..