Firebase从reducer中获取未定义,但是从reducer日志中声明为boolean

时间:2017-12-12 02:38:42

标签: firebase react-native redux redux-thunk

我有一个简单的Switch组件,可以在true和false之间来回切换。我已在我的操作中将其连接到Firebase中的表中设置此布尔值。 Firebase表示,第一个参数包含未定义的属性'。但是,当我记录该特定道具时,我可以看到它在切换时记录为真和假。道具已售出'。所有其他道具都设置得很好。

这是尝试设置为Firebase的动作创建者:

export const entrySave = ({ make, model, year, uid, image, sold }) => {
   const { currentUser } = firebase.auth();

   return (dispatch) => {
    firebase.database().ref(`/users/${currentUser.uid}/entries/${uid}`)
      .set({ make, model, year, image, sold })
         .then(() => {
            dispatch({ type: ENTRY_SAVE_SUCCESS });
         Actions.main({ type: 'reset' });
  });
 };
};

以下是更新this.props.sold更改的动作创建者:

export const entryUpdate = ({ prop, value }) => {
return {
type: ENTRY_UPDATE,
payload: { prop, value }

   };
  };

这里是Switch:

console.log(this.props.sold);
//both logging 'this.props.sold' and the redux debugger show that 
//this.props.sold is toggling between true and false
return (
  <View>
    <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', height: 66 }}>
      <View>
        <Text style={styles.switchLabelStyle}>Mark As Sold</Text>
      </View>
      <View style={{ paddingRight: 20 }}>
        <Switch
          tintColor='#a6a7a8'
          onValueChange={value => this.props.entryUpdate({ prop: 
             'sold', value })}
          value={this.props.sold}
        />
      </View>
    </View>

然后降下来:

const mapStateToProps = (state) => {
   const { make, model, year, sold } = state.entryForm;

   return { make, model, year, sold };
};

export default connect(mapStateToProps, { entryUpdate })(EmployeeForm);

这里是减速器:

 const INITIAL_STATE = {
   make: '',
   model: '',
   year: '',
   image: '',
   sold: false,
   uid: '',
   loading: false;
 };

export default (state = INITIAL_STATE, action) => {
   switch (action.type) {
     case ENTRY_UPDATE:
       return { ...state, [action.payload.prop]: action.payload.value };
     case ENTRY_CREATE:
       return INITIAL_STATE;
     case ENTRY_SAVE_SUCCESS:
       return INITIAL_STATE;
     case ENTRY_CLEAR:
       return INITIAL_STATE;

任何人都能看到我失踪的东西?提前谢谢!

1 个答案:

答案 0 :(得分:0)

愚蠢的我......我忘了在父组件的mapStateToProps函数中包含'sold'作为道具。

const mapStateToProps = (state) => {
  const { make, model, year, image, sold } = state.entryForm;

  return { make, model, year, image, sold };
};