2017-01-21 39 views
1

我用終極版 + 反應建立自己的網站,我想用Redux的控制側邊欄visible.The側邊欄被定義語義-UI-反應。因爲我想通過其他組件控制它,所以我在側邊欄的父組件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動作是平原對象。如果我不使用redux-thunk,我想我也不會遇到這個錯誤。所以,我很困惑,如何解決這個問題?

感謝所有幫助

+0

不確定這是相關的,因此我將它寫爲評論。你爲什麼使用'dispatch'?您應該在['connect']中使用'mapDispatchToProps'(https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options),並將操作作爲'this.props.actionName'。只要確保你使用的是道具版本而不是導入版本。多次犯這個錯誤:D –

+0

關於你的代碼的一般性評論,如果你已經在使用REDX,爲什麼要使用組件狀態?如果你使用的是REDX,最好是通過REDX完成所有的狀態管理,代碼更容易推理,測試更簡單。額外的好處是這樣,你不需要類,你可以使用純組件。 –

+0

@MarkoGrešak是的,我會解釋一下你說的。首先,我使用** dispatch **,因爲我指的是正式的Redux Reddit API代碼。第二,我使用的狀態只是用於活動的一個項目,這是語義 - 使用默認實現,所以我不會改變它。 – g1eny0ung

回答

9

這裏:

dispatch(sidebarVisibleAction)

您傳遞函數sidebarVisibleActiondispach。你應該把它和傳遞,故其代碼如下:

dispatch(sidebarVisibleAction())

(現在dispatch將得到的對象,不是一個函數)。

+0

很棒。我在尋找答案時正在看這個,但不知何故錯過了它。對於作者(誰提到他們指的是官方的Reddit API示例),這可以在這裏看到使用:http://redux.js.org/docs/advanced/ExampleRedditAPI.html#containersasyncappjs –

+1

是的,這是答案!謝謝!但我更改我的代碼,我發現一個新的錯誤:(0,_outside2.default)不是一個函數。並感謝@MarkoGrešak,我認爲你是對的,我改變我的代碼使用** mapDispatchToProps **並連接它。我試圖解決新的錯誤。 – g1eny0ung

+0

@ g1eny0ung這看起來像是錯誤地使用了'import'和/或'export'。很難說出位置,因爲你似乎沒有包含所有的代碼。如果你自己不明白,請發佈一個新問題。 –

相關問題