我尝试将状态连接到我的一个组件中的道具。当mapPropsToState(state)
被调用时,我可以看到这state
正是我想要的。但是,当我尝试记录道具时,我收到一条错误消息:
Uncaught ReferenceError: props is not defined
at eval (eval at _getCardAndBalance (VM28 Home.bundle:NaN), <anonymous>:1:1)
at _getCardAndBalance (VM28 Home.bundle:58)
at Object.onPress (VM28 Home.bundle:235)
at TouchableText.touchableHandlePress (Text.js:242)
at TouchableText._performSideEffectsForTransition (Touchable.js:880)
at TouchableText._receiveSignal (Touchable.js:779)
at TouchableText.touchableHandleResponderRelease (Touchable.js:491)
at onResponderRelease (Text.js:194)
at Object.invokeGuardedCallbackImpl (ReactNativeRenderer-dev.js:286)
at invokeGuardedCallback (ReactNativeRenderer-dev.js:497)
我不明白我在这里做错了什么以及为什么它不起作用。
这是我的减速机:
const initialState = {}
const calReducer = (state = initialState, action:any) => {
console.log('CAL reducer with type ' + action.type + ' token ' + action.token)
switch(action.type) {
case CAL_SEND_OTP_TOKEN:
return {...state, sendOtpToken: action.token}
case CAL_VERIFY_OTP_TOKEN:
return {...state, verifyOtpToken: action.token}
case CAL_GET_PAY_ACCOUNT:
return {...state, payAccount: action.cards}
default:
return state
}
}
export default calReducer;
这是我的组件:
const Home = (props: any) => {
useEffect(() => {
console.log("Home --> useEffect() props are: " + props)
}, [])
return (
<View style={styles.MainContainer}>
<ScrollView
scrollEventThrottle={17}
contentContainerStyle={{ paddingTop: Header_Maximum_Height }}
onScroll={Animated.event([
{ nativeEvent: { contentOffset: { y: AnimatedHeaderValue } } }
], { useNativeDriver: false })}>
<Text style={styles.TextViewStyle} onPress={ (e) => _getCardAndBalance(e)}>GetCardsAndBalance</Text>
</ScrollView>
);
};
const mapDispatchToProps = (dispatch:any) => ({
setCalSendOtpToken: (token: string) => dispatch(setCalSendOtpToken(token)),
})
const mapStateToProps = (state:any) => {
console.log("Home: --> mapStateToProps(): state is: " + state)
return {
sendOtpToken: state.calReducer.sendOtpToken,
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Home);
无论我在哪里用断点停止代码执行,道具总是没有定义。那个代码有什么问题?