【问题标题】:Firebase gets undefined from reducer, but state from reducer logs as a booleanFirebase 从reducer 中获取未定义,但reducer 日志中的状态为布尔值
【发布时间】:2018-05-25 15:53:33
【问题描述】:

我有一个简单的 Switch 组件,可以在真假之间来回切换。我已经在我的操作中将它连接起来,以便在 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 }

   };
  };

这是开关:

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;

有人看到我错过了什么吗?提前致谢!

【问题讨论】:

    标签: firebase react-native redux redux-thunk


    【解决方案1】:

    真傻...我忘了在父组件的 mapStateToProps 函数中包含“已售出”作为道具。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-21
      • 2017-05-31
      • 1970-01-01
      • 2021-10-28
      • 2021-01-01
      • 2017-03-07
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多