2017-03-02 91 views
-1

我試圖在REDX中更改複選框的狀態。除了最後一部分外,所有設置都正確。我嘗試發送一個「假」或「真」,並在行動中決定叫什麼,但我得到這個錯誤在REDX中將一個對象的值更改爲true或false

Uncaught TypeError: Cannot read property 'payload' of undefined 

這是我的代碼

export function isChecked(isCheck) { 

if(isCheck == false){ 
    return{ 
     type: "IS_TRUE", 
     isCheck 
    } 
} else if (isCheck == true){ 
    return{ 
     type: "IS_FALSE", 
     isCheck 
    } 
    } 
} 

如果我把剛纔別人那工作正常,但隨後圍繞它第二次不改變

回答

2

您使用終極版以錯誤的方式,這裏是複選框的例子:

// action 

export const SET_CHECKBOX_VALUE = 'SET_CHECKBOX_VALUE '; 
export function setChecked(checked) { 
return { 
    type: SET_CHECKBOX_VALUE, 
    checked 
} 
} 

// filter/Reducer 

import { SET_CHECKBOX_VALUE } from '..your action file here'; 

const initialCheckboxState = { isChecked: false }; 

export default function checkBoxFilter(state, action) { 
    switch (action.type) { 
    case SET_CHECKBOX_VALUE : 
     return { checked: action.checked} 
    default: 
     return initialCheckboxState; 
    } 
} 

// component 
import React, { Component } from 'react'; 
import { bindActionCreators } from 'redux'; 
import { connect } from 'react-redux'; 

import * as checkBoxActions from '..your action file here'; 

class MyComponent extends Component { 

    static propTypes = { 
    isChecked: React.PropTypes.bool.isRequired, 
    setChecked: React.PropTypes.func.isRequired 
    }; 

    render() { 
    return (
     <div onClick={() => { this.props.setChecked(!this.props.isChecked) }> 
     {this.props.isChecked} 
     </div> 
    ); 
    } 
} 

function mapStateToProps(state) { 
    return { 
    isChecked: state.checkBoxFilter.checked 
    }; 
} 

function mapDispatchToProps(dispatch) { 
    return bindActionCreators(checkBoxActions, dispatch); 
} 

export default connect(mapStateToProps, mapDispatchToProps)(MyComponent); 
相關問題