2017-09-14 118 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 –