Firebase 从 reducer 中获取未定义,但从 reducer 日志中状态为布尔值
Firebase gets undefined from reducer, but state from reducer logs as a boolean
我有一个简单的 Switch 组件,可以在 true 和 false 之间来回切换。我已将其连接到我的操作中,以便在 Firebase 中的 table 中设置此布尔值。 Firebase 表示 'first argument contains undefined in property'。但是,当我记录那个特定的 prop 时,我可以看到它在我切换时记录为 true 和 false。道具是'sold'。所有其他道具都设置好了。
这是尝试设置为 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;
有人看到我遗漏了什么吗?提前致谢!
愚蠢的我...我忘记在父组件的 mapStateToProps 函数中包含 'sold' 作为道具。
const mapStateToProps = (state) => {
const { make, model, year, image, sold } = state.entryForm;
return { make, model, year, image, sold };
};
我有一个简单的 Switch 组件,可以在 true 和 false 之间来回切换。我已将其连接到我的操作中,以便在 Firebase 中的 table 中设置此布尔值。 Firebase 表示 'first argument contains undefined in property'。但是,当我记录那个特定的 prop 时,我可以看到它在我切换时记录为 true 和 false。道具是'sold'。所有其他道具都设置好了。
这是尝试设置为 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;
有人看到我遗漏了什么吗?提前致谢!
愚蠢的我...我忘记在父组件的 mapStateToProps 函数中包含 'sold' 作为道具。
const mapStateToProps = (state) => {
const { make, model, year, image, sold } = state.entryForm;
return { make, model, year, image, sold };
};