react-native - React Native、Fetch 和 Dispatch : How do I access responseData within mapDispatchToProps()?

标签 react-native react-redux fetch dispatch

我正在从我的“扫描仪”组件中获取 componentWillMount() 中的 JSON 数据,如下所示:

async componentWillMount() {
  const url = 'https://foo.com/products/product1.json';

  fetch(url)
  .then((response) => response.json())
  .then((responseData) => this.props.dispatchProductLoad())
  .catch(error => {
    console.log(error);
  });
}

下面是我的发送代码:

function mapDispatchToProps(dispatch) {
  return { dispatchProductLoad: () => dispatch(productLoad(SOMEDATA)) };
};

export default connect(null, mapDispatchToProps)(Scanner);

变量 SOMEDATA 应该保存来自 responseData 的值(目前它还没有定义)

所以我的问题是 - 如何将 SOMEDATA 的值设置为 responseData 中保存的值?

最佳答案

您可以使用 responseData 作为参数调用 action creator 并定义您的 mapDispatchToProps 函数

async componentWillMount() {
  const url = 'https://foo.com/products/product1.json';

  fetch(url)
  .then((response) => response.json())
  .then((responseData) => this.props.dispatchProductLoad(responseData))
  .catch(error => {
    console.log(error);
  });
}


function mapDispatchToProps(dispatch) {
  return { dispatchProductLoad: (response) => dispatch(productLoad(response)) };
};

export default connect(null, mapDispatchToProps)(Scanner);

关于react-native - React Native、Fetch 和 Dispatch : How do I access responseData within mapDispatchToProps()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49034706/

相关文章:

javascript - Android 版本上的 React Native 应用程序显示 "Unable to load script from assets' index.android.bundle'“需要重新启动系统

javascript - React Native w/redux : dispatch is not a function

php - 使用数据库 TEXT to IMAGE 使用 mysqli_fetch_row

jquery - 如何在 fetch/axios 跨站请求上使用 JSONP

javascript - currentArray.forEach 不是函数(Fetch API)

ios - 无法查看Firebase Analytics调试 View React Native

android - 如何在 Google Play 上的同一应用程序中使用另一个 keystore

javascript - 包 react-native-onesignal 已被忽略,因为它包含无效配置

javascript - 如何使用 React 动态处理多个过滤器?

javascript - 为什么函数作为 React 子函数无效?