首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用App类进行渲染的Redux thunk

使用App类进行渲染的Redux thunk
EN

Stack Overflow用户
提问于 2019-05-04 10:41:37
回答 1查看 119关注 0票数 0

使用App class渲染Redux thunk -我使用react-native和redux thunk通过App class的componentDidMount调用调度程序,并收到错误"props is not defined“和"Unable to find module for EventDispatcher”。将在这方面请求专家的进一步帮助。

index.js

代码语言:javascript
复制
import React from 'react';
import {AppRegistry} from 'react-native';

import {Provider} from 'react-redux';
import configureStore from './configureStore';
import App from './App';
import {name as appName} from './app.json';

const store = configureStore();

const rnredux = () => (
    <Provider store={store}>
        <App />
    </Provider>
)

AppRegistry.registerComponent(appName, () => rnredux);

app.js

代码语言:javascript
复制
import React from 'react';
import {Platform, TouchableHighlight, StyleSheet, Text, View} from 'react-native';
import {connect} from 'react-redux'
import {fetchPeopleFromAPI} from './actions'


class App extends React.Component {
  componentDidMount() {
    this.props.getPeople();
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native! & Redux</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <TouchableHighlight onPress={props.getPeople} style={styles.buttonText}>
          <Text>Fetch Data</Text>
        </TouchableHighlight>
        {
          isFetching && <Text>Loading</Text>
        }
        {
          people.length? (
            people.map((person,index) => {
              return (
                <View key={index}>
                 <Text>Name: {person.breedName}</Text>
                </View>
              )
            })
           ) : null
          }
      </View>
    );
  }
}

const {people, isFetching} = props.people
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  buttonText: {
    backgroundColor: 'red',
    height:60,
    justifyContent: 'center',
    alignItems: 'center',
  }
});

function mapStateToProps (state) {
  return {
    people: state.people
  }
}

function mapDispatchToProps (dispatch) {
  return {
    getPeople: () => dispatch(fetchPeopleFromAPI())
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App)
EN

回答 1

Stack Overflow用户

发布于 2019-05-04 23:10:58

从您的代码中完全删除此部分:

代码语言:javascript
复制
function mapDispatchToProps (dispatch) {
  return {
    getPeople: () => dispatch(fetchPeopleFromAPI())
  }
}

在您的导出声明中,修改为:

代码语言:javascript
复制
export default connect(
  mapStateToProps,
 { fetchPeopleFromAPI }
)(App)

最后在你的componentDidMount

代码语言:javascript
复制
componentDidMount() {
    this.props.fetchPeopleFromAPI();
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55979030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档