【发布时间】:2017-10-09 20:14:31
【问题描述】:
好的,情况如下:
我正在使用 Redux 开发 React Native 应用程序,我想使用 FlatList 显示 GET 请求(使用 Axios 生成)的结果。但是,当我尝试使用 FlatList 呈现该数据时,我收到一个错误,并且什么都没有显示!我确定正在返回数据,因为我可以使用普通的 Text 组件显示它,但我需要能够将其全部放入 FlatList 中。
完全被以下错误弄糊涂了: screenshot of error
我正在调度以进行调用的操作如下(我正在使用 promise 中间件):
{
type: 'PROPOSALS_FETCH_PAGE',
payload: axios.get(base + '/proposals/get_recent?page=1'),
}
我的reducer代码如下:
const defaultState = {
fetching: false,
fetched: false,
proposalList: [],
error: null,
};
const proposalReducer = (state=defaultState, action) => {
switch (action.type) {
case "PROPOSALS_FETCH_PAGE_PENDING":
state = {...state, fetching: true, fetched: false};
break;
case "PROPOSALS_FETCH_PAGE_FULFILLED":
state = {...state, fetching: false, fetched: true, proposalList: action.payload.data.data.proposals};
break;
case "PROPOSALS_FETCH_PAGE_REJECTED":
state = {...state, fetching: false, fetched: false, error: action.payload.message};
break;
default: break;
}
return state;
}
export default proposalReducer;
最后,我用来实际显示所有这些的智能组件:
import Proposal from './Proposal'
class ProposalFeed extends Component {
constructor(props) {
super(props);
props.dispatch(fetchProposalPage(1));
}
render() {
const proposals = this.props.proposal.proposalList.map(
proposal => ({...proposal, key:proposal.id})
);
return (
<View>
<FlatList
data={proposals}
renderItem={({proposal}) => (<Proposal
name={proposal.name}
summary={proposal.summary}
/>)}
/>
</View>
);
}
}
export default connect((store) => {
return {
proposal: store.proposal,
}
})(ProposalFeed);
还有愚蠢的“提案”组件!
import styles from './Styles';
class Proposal extends Component {
render() {
return (
<View style={styles.horizontalContainer}>
<View style={styles.verticalContainer}>
<Text style={styles.name}>{this.props.name}</Text>
<Text style={styles.summary}>{this.props.summary}</Text>
<View style={styles.bottom}></View>
</View>
</View>
)
}
}
export default Proposal;
任何帮助将不胜感激!我一直在寻找解决此问题的方法,但绝对没有任何效果。我也担心问题可能是我正在使用的中间件(redux-promise-middleware),但我不确定。
【问题讨论】:
-
看起来它正在尝试在返回数据之前进行渲染...您是否尝试过使用 ActivityIndicator 加载块,并在收到响应后相应地设置状态?
标签: javascript react-native react-redux axios