在mapStateToProps
没有得到来自国家store
。console.log的响应是:
Object {
"selectStateClassify": Object {
"newClassify": "É pra vir algo",
},
}
并显示一条消息错误,说Can't find variable: stateClassify
,我知道这是一个属性,但我不知道如何像使用对象一样使用此属性。我想stateClassify
在标签中使用<Text>{stateClassify}</Text>
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
import { connect } from 'react-redux';
function Classificacao({ navigation }){
return(
<View style={{
flex: 1,
paddingTop: 30,
flexDirection: 'column',
alignItems: 'center',
}}
>
<Text style={{
textAlign: 'center',
paddingHorizontal: 60,
fontSize: 25,
}}
>Com base no Manual de Quadros do AIDPI, o paciente pode apresentar:</Text>
<Text style={{
textAlign: 'center',
paddingHorizontal: 60,
marginTop: 80,
fontSize: 25,
fontWeight: 'bold',
}}
>{stateClassify}</Text>
<TouchableOpacity
style={styles.goButton}
onPress={() => { navigation.navigate('Doencas'); }}
>
<Text style={styles.textButton}>Resultado</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({...});
const mapStateToProps = state => {
console.log(state);
return{
stateClassify: state.selectStateClassify.newClassify,
}
};
export default connect(mapStateToProps)(Classificacao);
我发现的另一个问题是在reducer上,我不知道这是什么问题,但是reducer没有获得用于更改状态的操作参数,代码如下:
import { SHOW_CLASSIFY } from '../actions/type';
const initialState = {
newClassify: 'É pra vir algo'
}
export const selectClassify = (state = initialState, action) => {
switch(action.type){
case SHOW_CLASSIFY:
return{
...state,
newClassify: action.newClassify
};
default:
return state;
}
}
动作代码:
import { SHOW_CLASSIFY } from './type';
export const showClassify = (classify) => ({
type: SHOW_CLASSIFY,
newClassify: classify
});
商店代码:
import { createStore } from 'redux';
import { Reducers } from '../reducers/index';
export const Store = createStore(Reducers);
App.js代码:
import React from 'react';
import { StatusBar } from 'react-native';
import { Provider } from 'react-redux';
import Routes from './src/routes';
import { Store } from './src/redux/store/store';
export default function App() {
return (
<>
<Provider store={Store}>
<StatusBar barStyle="light-content" backgroundColor="#f89747" />
<Routes />
</Provider>
</>
);
}
将stateClassify添加到已分解的props中,以便可以像这样使用它,请记住,您正在将props映射到您的组件,但是当您在函数参数中分解props时,还需要添加它。
function Classificacao({ navigation, stateClassify }){
...
}
它确实有效,谢谢!但是减速器仍然不阅读动作参数。一个
console.log(action.type)
前switch
表演undefined
。您如何调度动作,我看到您的化简器代码,但没有看到您调度动作的位置?