我正在构建我的第一个 React Native 应用程序,并使用 Redux 作为应用程序内部的数据流。
我想从我的 Parse 加载一些数据后端并将其显示在 ListView
上。我目前唯一的问题是,由于某种原因,我使用 fetch() 创建的请求实际上并未被触发。我浏览了 Redux docs 中的文档和示例还读了这篇非常好的blog post 。他们基本上做了我想要实现的目标,但我不知道我的实现与他们的代码示例有何不同。
这是我目前的设置(缩短以仅显示相关部分):
概览RootComponent.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
,但实际上似乎并没有关闭请求。我不太确定如何进一步调试这个问题,或者引用哪些资源来帮助我解决这个问题。
最佳答案
我假设 Redux 期待的是 Promise 而不是函数。这是真的吗?
如果是这样,我认为您的返回函数可能无法正常工作。
返回后会出现一个新行,JavaScript 可能会(有帮助地)在其中插入分号。
请参见此处:Why doesn't a Javascript return statement work when the return value is on a new line?
关于reactjs - 使用 React Native + Redux 获取数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015787/