Javascript MapStateTrops状态未定义
我在mapStateToProps中有一个状态未定义的问题,这简直让我发疯。 Stackoverflow中有很多未定义的问题,但是我使用的代码到目前为止符合所有标准,但是仍然没有定义 我的代码是基于 我正在使用React native和Redux的新版本。 “反应”:“16.11.0”, “反应本机”:“0.62.2”, “react redux”:“^7.2.0”, “redux”:“^4.0.5” 这些是文件 index.jsJavascript MapStateTrops状态未定义,javascript,reactjs,react-native,react-redux,jsx,Javascript,Reactjs,React Native,React Redux,Jsx,我在mapStateToProps中有一个状态未定义的问题,这简直让我发疯。 Stackoverflow中有很多未定义的问题,但是我使用的代码到目前为止符合所有标准,但是仍然没有定义 我的代码是基于 我正在使用React native和Redux的新版本。 “反应”:“16.11.0”, “反应本机”:“0.62.2”, “react redux”:“^7.2.0”, “redux”:“^4.0.5” 这些是文件 index.js import React from 'react'; impor
import React from 'react';
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import {Provider} from 'react-redux';
import configStore from './app/store';
const store = configStore();
const TestApp = () =>
<Provider store={store}>
<App/>
</Provider>
AppRegistry.registerComponent(appName, () => TestApp);
app/store.js
import {createStore, combineReducers} from 'redux';
import chatReducer from './reducers/chatReducer'
const rootReducer = combineReducers({
chatReducer:chatReducer
})
const configureStore = () => createStore(rootReducer);
export default configureStore;
app/reducers/chatdreducer.js
import {CREATE_CHAT,DELETE_CHAT} from '../actions/types';
const initialState = {
chatList: [],
}
const chatReducer = (state = initialState, action) => {
switch(action.type){
case CREATE_CHAT:
return {
...state,
chatList: state.chatList.concat({
_id: Math.random().toString(12).substring(0) ,
text: action.data,
createdAt: new Date(),
user: {
_id: 1,
},
sent: true,
received: true,
pending: true,
})
}
case DELETE_CHAT:
return {
...state,
chatList: state.chatList.filter((item) =>
item.key !== key)
}
default:
return state;
}
};
export default chatReducer;
app/actions/types.js
import {CREATE_CHAT,DELETE_CHAT} from './types';
export const createChat = (data) => (
{
type: CREATE_CHAT,
data: data
}
);
export const deleteChat = (key) => (
{
type: DELETE_CHAT,
key: key
}
);
export const CREATE_CHAT = 'ADD_CHAT';
export const DELETE_CHAT = 'DELETE_CHAT';
app/actions/types.js
import {CREATE_CHAT,DELETE_CHAT} from './types';
export const createChat = (data) => (
{
type: CREATE_CHAT,
data: data
}
);
export const deleteChat = (key) => (
{
type: DELETE_CHAT,
key: key
}
);
export const CREATE_CHAT = 'ADD_CHAT';
export const DELETE_CHAT = 'DELETE_CHAT';
这个.props不知何故也不起作用,它确实令人沮丧,因为redux中有许多不同版本的实现
我真的希望有人知道如何解决这个问题
提前谢谢。您没有使用
道具
您可以从以下道具获取数据:
const App: () => React$Node = (props) => {
return (
<>
<SafeAreaView>
<Text>{this.props.chats.length}</Text>
<Button onPress={addItem} title="Add Msg"/>
</SafeAreaView>
</>
);
};
const-App:()=>React$Node=(道具)=>{
返回(
{this.props.chats.length}
);
};
mapStateToProps
负责通过connect
功能将redux store的数据传递给组件props
看起来你的论点顺序不对
导出默认连接(mapStateToProps、mapDispatchToProps)(应用程序)代码>感谢您的快速回复,非常感谢。我已经试过了,但它仍然给了我一些未定义的道具。有什么解决办法吗?我真的很喜欢这个,谢谢你的快速回复和正确的答案(非常感谢:)。