2017-09-14 119 views
1

取東西我一直有麻煩試圖建立終極版,我已經mapStateToProps和mapDispatchToProps設置和我的動作和減速,但每當我試圖輸出任何我映射的道具我不能獲取從道具我有什麼關係映射狀態。廣東話從終極版

我已經回到我的index.js並嘗試使用alert(store.getState())和alert(store.getState()。value1)和alert(store.getState(value1))之後我知道value1已被派遣,通過我的行動,我的減速器,然後(希望)通過我的商店。我也曾嘗試在mapStateToProps中使用的prop上使用警報

我得到的唯一輸出是store.getState()的空白警報,對於其他選項未定義。

我不知道什麼是我的代碼腳麻:(

動作/ index.js:

export const updateDial1 = (value1, value2) => { 
    return { 
    type: 'UPDATE_DIAL_1', 
    value1, 
    value2 
    } 
} 

減速器/ reducer.js

const update = (state, mutations) => Object.assign({}, state, mutations) 

const valuesReducer = (state = [], action) => { 
    switch (action.type) { 
     case 'UPDATE_DIAL_1': 
     { 
     return [ 
      ...state, 
      { 
       value1: action.value1, 
       value2: action.value2, 
      } 
     ] 
    } 
} 
export default valuesReducer 

組件/組件。 js

(取出組件)

// use this to apply values from redux store to local props 
const mapStateToProps = state => ({ 
    valuesReducer: state 
}) 

// use this to send changes to the Redux store 
const mapDispatchToProps = { 
    updateDialLine1: actions.updateDial1 
} 

export default connect(mapStateToProps, mapDispatchToProps)(CustomComponent)

與商店聲明index.js:(所述的console.log返回一個空數組)

import React, { Component } from 'react'; 
import { Root } from './navigator/router'; 

import { render } from 'react-dom' 
import { Provider } from 'react-redux' 
import { createStore } from 'redux' 
import valuesReducer from './reducers/dials' 

let store = createStore(valuesReducer) 

export default class App extends Component { 
    render() { 

     // setTimeout(() => {console.log(store)}, 15000) 

     return (
      <Provider store={store}> 
       <Root /> 
      </Provider> 
     ); 
    } 
} 
+3

在一個側面節點,而不是調試與'警報()','青睞的console.log()' 。在目前情況下更好,請在瀏覽器中安裝redux擴展。 – Delapouite

+0

您可以添加商店初始化嗎?我想看看你如何註冊你的減速器。 – Okazari

+0

現在就爲你添加。 –

回答

1

在您的減速器代碼示例,直接分配一個數組[]以狀態對象。您應該將您的減速器代碼更改爲以下內容:

const valuesReducer = (state = {}, action) => { 
    switch (action.type) { 
     case 'UPDATE_DIAL_1': 
     { 
     return { 
      ...state, 
      { 
       value1: action.value1, 
       value2: action.value2, 
      } 
     } 
    } 
} 

請注意在減速器中使用{}而不是[]。

如果您需要在您的狀態對象的數組然後將其添加爲一個對象的屬性:

const valuesReducer = (state = {myArray:[ ]}, action) => {return state} 
+0

我不確定這裏的問題,我已經使用數組作爲reducer根狀態,並且它非常好。 – Okazari

+0

我其實剛剛在30分鐘前得到了所有的修正,但是這個答案是正確的:L –