redux动作必须是普通对象,但我定义了普通对象

g1e*_*ung 6 javascript reactjs redux redux-thunk react-redux

我使用redux + react来构建我的网站,我想使用redux来控制侧边栏可见.侧边栏是由semantic-ui-react定义的.因为我想控制它跨越另一个组件,所以我在侧边栏中定义了道具父组件const { visible, dispatch } = this.props,有一个onClick函数来处理这个.我会展示我的代码.

未捕获的错误:操作必须是普通对象.使用自定义中间件进行异步操作.

这个错误让我困惑了一个下午,我不知道为什么!这是我的行动代码:

**action**

export const SIDEBARISVISIBLE = 'sidebarVisible'

export function sidebarVisibleAction() {
  return { type: SIDEBARISVISIBLE }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我定义了一个动作创建者返回了一个普通对象.

这是我的reducer代码:

**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
Run Code Online (Sandbox Code Playgroud)

我的商店代码:

**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)
  )
}
Run Code Online (Sandbox Code Playgroud)

然后,我的组件代码(部分):

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)
Run Code Online (Sandbox Code Playgroud)

最后,我的路由器:

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>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我搜索这个错误的答案,大多数人都说,你必须设置redux-thunk并使用ApplyMiddleware来使用它,这是最简单的方法.但我定义的动作是普通对象.如果我不使用redux- thunk,我想我也不会遇到这个错误.所以,我很困惑,怎么解决这个问题?

谢谢你的帮助

Mar*_*ski 23

这里:

dispatch(sidebarVisibleAction)

你逝去的功能sidebarVisibleActiondispach.您应该调用它并传递结果,因此代码将如下所示:

dispatch(sidebarVisibleAction())

(现在dispatch将获得对象,而不是函数).