【发布时间】: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