2017-03-13 59 views
2

有人能告訴我爲什麼state.set導致未定義。Redux state.set導致未定義

我有一個減速,像這樣:

const initialState = fromJS({ 
    description: 'default_description', 
}); 

function helpReducer(state = initialState, action) { 
    switch (action.type) { 
    case DEFAULT_ACTION: 
     return state; 
    case CHANGE_DESCRIPTION: 
     console.log('Change Description reducer action.data: '+action.data); //results in string say foo 
     console.log('state: '+state); //results in valid state on first call 
     return state 
     .set('description':action.data); 
    default: 
     return state; 
    } 
} 

我第一次打電話的動作CHANGE_DESCRIPTION,state = default_description

致電state.setstate.description = undefined

我以前使用過state.set,但我不確定它現在失敗的原因。

的行動被稱爲一個子組件,如下所示:

家長:

export class FOO extends React.PureComponent { 
    constructor(props){ 
    super(props); 
    } 
    render() { 
    return (
     <div> 
     <FOOForm onChange={this.props.changeDescription} /> 
     </div> 
    ); 
    } 
} 

function mapDispatchToProps(dispatch) { 
    return { 
    dispatch, 
    changeDescription: (e) => dispatch(changeDescription(e.target.value)) 
    }; 
} 

富表格演示的緣故:

function fooForm(props){ 
return <FormControl onChange={props.onChange} componentClass="textarea" />; 

} 
+0

沒有看到其餘的代碼,我會假設action.data是未定義的。你可以在交換機之前使用console.log'action.data'來查看它是否持有你想要的內容/ –

+0

Action.data是明確定義的。當我console.log每次我得到正確的值。 – Turtle

+0

爲什麼不只是'''返回action.data'''。它會自動改變減速器狀態。就像在文檔中http://redux.js.org/docs/basics/Reducers.html – Bonanza

回答

1

我不知道的上下文中你實現你的reducer,但我相信不變JS的語法應該使你的設置看起來像這樣:

return state.set('description', action.data)

請注意,您需要傳遞屬性和值作爲參數,而不是作爲鍵值對。

+0

Aaa我相信你是對的!謝謝!! – Turtle

+0

太棒了!很高興能幫到你! –