javascript - TypeError:store.getState 不是函数。(在 'store.getState()' 中,'store.getState' 未定义
问题描述
我一直在尝试将 Redux 集成到一个项目中。
我按照使用示例进行操作,但出现错误store.getState is not a function.
所以我知道其他人也问过类似的问题,但情况略有不同。
环境
OS: macOS Mojave 10.14
Node: 8.11.3
Yarn: 1.10.1
npm: 6.4.1
Watchman: 4.9.0
Xcode: Xcode 10.1
反应原生 cli:2.0.1 反应原生:0.57.2
索引.js
import React, {Component} from 'react';
import { AppRegistry } from 'react-native';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import promiseMiddleware from 'redux-promise';
import reducers from './src/Stores/reducers';
import App from './App';
import { name as appName } from './app.json';
const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore)
const appRedux = () => (
<Provider store={createStoreWithMiddleware}>
<App />
</Provider>
)
AppRegistry.registerComponent(appName, () => appRedux);
应用程序.js
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Cards />
</View>
);
}
}
Card_reducer.js
export default function(state={}, action){
switch(action.type){
case 'GET_ARTICLES':
return{...state, cards:action.payload}
default:
return state;
}
}
卡片 > index.js
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import { connect } from 'react-redux';
import { getCards } from '../Stores/actions'
import { bindActionCreators } from 'redux';
class Cards extends Component{
componentDidMount(){
this.props.getCards()
}
render(){
return(
<Text>Cartes</Text>
)
}
}
function mapStateToProps(state){
console.log(state);
return{
cards: state.cards
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({getCards}, dispatch);
}
export default connect(mapStateToProps,mapDispatchToProps)(Cards);
减速器 > Index.js
import { combineReducers } from 'redux';
import cards from './card_reducer'
const rootReducer = combineReducers({
cards
});
export default rootReducer;
谢谢您的帮助。
解决方案
我忘记在 appRedux 上使用减速器:/
const appRedux = () => (
<Provider store={createStoreWithMiddleware}>
<App />
</Provider>
)
变得
const appRedux = () => (
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
)
推荐阅读
- powershell - 一次添加 20 个通用计算机对象
- python - 逐行修改 pandas DataFrame 中的字符串
- android - Android Studio 3.2 不知道 Jetifier
- jquery - jquery在页面上注册多个按键事件
- javascript - D3.js 气泡图:Cx 和 cy 属性函数无法读取 lat 和 lon 坐标
- binary - 二进制加法溢出和求和正确性
- sql - SQL Server:从 GROUPED BY 结果集中选择特定行
- html - 使用 SVG 淡入文本
- javascript - SAPUI5: Add properties to Column added via View Settings
- php - 使用PHP显示来自mysql查询的奇数和偶数行数据