redux Actions 必须是普通对象,但我定义了普通对象
redux Actions must be plain objects but I defined plain object
我用redux + react build我的网站,我想用redux控制一个sidebar visible.The sidebar is defined by semantic-ui-react.Because 我想跨另一个组件控制它,所以我在 sidebar 的父级中定义了 props组件 const { visible, dispatch } = this.props
,有一个 onClick 函数来处理这个问题。我会展示我的代码。
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
这个错误把我搞糊涂了一个下午,我也不知道为什么!这是我的操作代码:
**action**
export const SIDEBARISVISIBLE = 'sidebarVisible'
export function sidebarVisibleAction() {
return { type: SIDEBARISVISIBLE }
}
如您所见,我定义了一个返回普通对象的动作创建器。
这是我的减速器代码:
**reducer**
import SIDEBARISVISIBLE from '../actions/outside.js'
function sidebarVisible(state = {
sidebarIsVisible: false
}, action) {
switch (action.type) {
case SIDEBARISVISIBLE:
return Object.assign({}, state, {
sidebarIsVisible: !state.sidebarIsVisible
})
default:
return state
}
}
export default sidebarVisible
还有我的商店代码:
**store**
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import sidebarVisible from '../reducers/outside.js'
export default initialState => {
return createStore(
sidebarVisible,
initialState,
applyMiddleware(thunk)
)
}
那么,我的组件代码(部分):
class OutsideView extends Component {
constructor(props) {
super(props)
this.state = { activeItem: '' }
}
handleItemClick = (e, { name }) => this.setState({ activeItem: name })
render() {
const { activeItem } = this.state
const { visible, dispatch } = this.props
return (
<div>
<SidebarNav visible={ visible } />
......
<Menu.Item
name='sidebar'
active={ activeItem === 'sidebar'}
onClick={ (e, {name}) => {
this.setState({ activeItem: name })
dispatch(sidebarVisibleAction)
} }>
<Icon color='red' name='list' />
</Menu.Item>
OutsideView.PropTypes = {
visible: PropTypes.bool.isRequired,
dispatch: PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
visible: state.sidebarIsVisible,
}
}
export default connect(
mapStateToProps
)(OutsideView)
最后,我的路由器:
import configureStore from './store/index.js'
const store = configureStore()
export default class Root extends Component {
render() {
return (
<Provider store={ store }>
<Router history={ browserHistory }>
<Route path="/" component={ OutsideRedux }>
<Route path='register' component={ Register } />
<Route path='login' component={ Login } />
<Route path='blog' component={ Blog } />
<Route path='about' component={ About } />
<Route path='home' component={ Home } />
<Route path='ask' component={ Ask } />
<Route path='panel' component={ Panel } />
<Route path='setting' component={ Setting } />
<Route path='user' component={ User } />
</Route>
</Router>
</Provider>
)
}
}
所以,我搜索这个错误的答案,大多数人说,你必须设置 redux-thunk 并使用 ApplyMiddleware 来使用它,最简单的 way.But 我定义的action是plain object。如果我不使用redux-thunk,我想我不会遇到这个错误either.So,我很迷茫,怎么办解决这个问题?
感谢大家的帮助
这里:
dispatch(sidebarVisibleAction)
您正在将函数 sidebarVisibleAction
传递给 dispach
。您应该调用它并传递结果,因此代码将如下所示:
dispatch(sidebarVisibleAction())
(现在 dispatch
将获取对象,而不是函数)。
我用redux + react build我的网站,我想用redux控制一个sidebar visible.The sidebar is defined by semantic-ui-react.Because 我想跨另一个组件控制它,所以我在 sidebar 的父级中定义了 props组件 const { visible, dispatch } = this.props
,有一个 onClick 函数来处理这个问题。我会展示我的代码。
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
这个错误把我搞糊涂了一个下午,我也不知道为什么!这是我的操作代码:
**action**
export const SIDEBARISVISIBLE = 'sidebarVisible'
export function sidebarVisibleAction() {
return { type: SIDEBARISVISIBLE }
}
如您所见,我定义了一个返回普通对象的动作创建器。
这是我的减速器代码:
**reducer**
import SIDEBARISVISIBLE from '../actions/outside.js'
function sidebarVisible(state = {
sidebarIsVisible: false
}, action) {
switch (action.type) {
case SIDEBARISVISIBLE:
return Object.assign({}, state, {
sidebarIsVisible: !state.sidebarIsVisible
})
default:
return state
}
}
export default sidebarVisible
还有我的商店代码:
**store**
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import sidebarVisible from '../reducers/outside.js'
export default initialState => {
return createStore(
sidebarVisible,
initialState,
applyMiddleware(thunk)
)
}
那么,我的组件代码(部分):
class OutsideView extends Component {
constructor(props) {
super(props)
this.state = { activeItem: '' }
}
handleItemClick = (e, { name }) => this.setState({ activeItem: name })
render() {
const { activeItem } = this.state
const { visible, dispatch } = this.props
return (
<div>
<SidebarNav visible={ visible } />
......
<Menu.Item
name='sidebar'
active={ activeItem === 'sidebar'}
onClick={ (e, {name}) => {
this.setState({ activeItem: name })
dispatch(sidebarVisibleAction)
} }>
<Icon color='red' name='list' />
</Menu.Item>
OutsideView.PropTypes = {
visible: PropTypes.bool.isRequired,
dispatch: PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
visible: state.sidebarIsVisible,
}
}
export default connect(
mapStateToProps
)(OutsideView)
最后,我的路由器:
import configureStore from './store/index.js'
const store = configureStore()
export default class Root extends Component {
render() {
return (
<Provider store={ store }>
<Router history={ browserHistory }>
<Route path="/" component={ OutsideRedux }>
<Route path='register' component={ Register } />
<Route path='login' component={ Login } />
<Route path='blog' component={ Blog } />
<Route path='about' component={ About } />
<Route path='home' component={ Home } />
<Route path='ask' component={ Ask } />
<Route path='panel' component={ Panel } />
<Route path='setting' component={ Setting } />
<Route path='user' component={ User } />
</Route>
</Router>
</Provider>
)
}
}
所以,我搜索这个错误的答案,大多数人说,你必须设置 redux-thunk 并使用 ApplyMiddleware 来使用它,最简单的 way.But 我定义的action是plain object。如果我不使用redux-thunk,我想我不会遇到这个错误either.So,我很迷茫,怎么办解决这个问题?
感谢大家的帮助
这里:
dispatch(sidebarVisibleAction)
您正在将函数 sidebarVisibleAction
传递给 dispach
。您应该调用它并传递结果,因此代码将如下所示:
dispatch(sidebarVisibleAction())
(现在 dispatch
将获取对象,而不是函数)。