[英]Data fetching with React Native + Redux not working
I am building my first React Native app and use Redux for the data flow inside my app.我正在构建我的第一个 React Native 应用程序并使用 Redux 来处理我的应用程序内的数据流。
I want to load some data from my Parse backend and display it on a ListView
.我想从Parse后端加载一些数据并将其显示在
ListView
。 My only issues at the moment is that for some reason, the request that I create using fetch()
for some reason isn't actually fired.我目前唯一的问题是,出于某种原因,我出于某种原因使用
fetch()
创建的请求实际上并未被触发。 I went through the documentation and examples in the Redux docs and also read this really nice blog post .我浏览了Redux 文档中的文档和示例,还阅读了这篇非常好的博客文章。 They essentially do what I am trying to achieve, but I don't know where my implementation differs from their code samples.
他们基本上做了我想要实现的目标,但我不知道我的实现与他们的代码示例有何不同。
Here is what I have setup at the moment (shortened to show only relevant parts):这是我目前的设置(缩短以仅显示相关部分):
class OverviewRootComponent extends Component {
componentDidMount() {
const { dispatch } = this.props
dispatch( fetchOrganizations() )
}
}
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)
})
}
}
When I am calling dispatch( fetchOrganizations() )
like this, I do see the logs until Actions - return promise
, but it doesn't seem to actually to fire off the request.当我像这样调用
dispatch( fetchOrganizations() )
,我确实看到了日志,直到Actions - return promise
,但它似乎实际上并没有触发请求。 I'm not really sure how how I can further debug this or what resources to consult that help me solve this issue.我不确定如何进一步调试或咨询哪些资源来帮助我解决这个问题。
I'm assuming that Redux is expecting a Promise rather than a function.. Is that true?我假设 Redux 期待的是一个 Promise 而不是一个函数。这是真的吗?
If so, I think your return function may not be working.如果是这样,我认为您的返回功能可能不起作用。
You have a new line after your return, and it's possible JavaScript is (helpfully) inserting a semicolon there.您在返回后有一个新行,并且 JavaScript 可能(有帮助地)在那里插入一个分号。
See here: Why doesn't a Javascript return statement work when the return value is on a new line?请参阅此处: 当返回值位于新行时,为什么 Javascript 返回语句不起作用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.