gpt4 book ai didi

javascript - redux Actions 必须是普通对象,但我定义了普通对象

转载 作者:数据小太阳 更新时间:2023-10-29 05:41:37 24 4
gpt4 key购买 nike

我使用redux + react来搭建我的网站,我想用redux来控制一个侧边栏是否可见。侧边栏是由semantic-ui-react定义的。因为我想跨另一个组件来控制,所以我在sidebar的父组件const { visible, dispatch } = this.props中定义了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 }
}

如您所见,我定义了一个返回普通对象的 Action 创建者。

这是我的 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

还有我的商店代码:

**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 来使用它,这是最简单的方法。但是我定义的操作是 < strong>plain object。如果我不使用redux-thunk,我想我也不会遇到这个错误。所以,我很困惑,如何解决这个问题?

感谢大家的帮助

最佳答案

这里:

调度(sidebarVisibleAction)

您正在将函数 sidebarVisibleAction 传递给 dispach。您应该调用它并传递结果,因此代码将如下所示:

调度(sidebarVisibleAction())

(现在 dispatch 将得到对象,而不是函数)。

关于javascript - redux Actions 必须是普通对象,但我定义了普通对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41780381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com