【问题标题】:Data fetching with React Native + Redux not working使用 React Native + Redux 获取数据不起作用
【发布时间】:2016-07-01 03:48:42
【问题描述】:

我正在构建我的第一个 React Native 应用程序并使用 Redux 处理我的应用程序内的数据流。

我想从我的Parse 后端加载一些数据并将其显示在ListView 上。目前我唯一的问题是,由于某种原因,我使用fetch() 创建的请求实际上并没有被触发。我浏览了Redux docs 中的文档和示例,还阅读了这个非常好的blog post。他们基本上做了我想要达到的目标,但我不知道我的实现与他们的代码示例有什么不同。

这是我目前的设置(缩短以仅显示相关部分):

OverviewRootComponent.js

class OverviewRootComponent extends Component {
  componentDidMount() {
    const { dispatch } = this.props
    dispatch( fetchOrganizations() )
  }
}

Actions.js

export const fetchOrganizations = () => {
  console.log('Actions - fetchOrganizations');
  return (dispatch) => {
    console.log('Actions - return promise');
    return
      fetch('https://api.parse.com/1/classes/Organization', {
        method: 'GET',
        headers: {
          'X-Parse-Application-Id': 'xxx',
          'X-Parse-REST-API-Key': 'xxx',
        }
      })
      .then( (response) => {
        console.log('fetchOrganizations - did receive response: ', response)
        response.text()
      })
      .then( (responseText) => {
        console.log('fetchOrganizations - received response, now dispatch: ', responseText);
        dispatch( receiveOrganizations(responseText) )
      })
      .catch( (error) => {
        console.warn(error)
      })
  }
}

当我像这样调用dispatch( fetchOrganizations() ) 时,我确实看到了直到Actions - return promise 的日志,但它似乎并没有真正触发请求。我不确定如何进一步调试此问题或咨询哪些资源可以帮助我解决此问题。

【问题讨论】:

标签: reactjs promise react-native fetch redux


【解决方案1】:

我假设 Redux 期望的是 Promise 而不是函数。这是真的吗?

如果是这样,我认为您的返回功能可能不起作用。

您返回后有一个新行,JavaScript 可能(有帮助地)在那里插入一个分号。

请看这里:Why doesn't a Javascript return statement work when the return value is on a new line?

【讨论】:

  • 哇... js 鬼鬼祟祟地在我不知情的情况下插入分号... 你无法编造这些东西! :D 非常感谢您的提示!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 2021-12-16
相关资源
最近更新 更多