我正在使用React Native應用程序。在應用程序中,當用戶登錄時,會向服務器發出API調用,以使用redux-saga來獲取數據。在redux存儲中,我維護一個布爾變量「fetchingData」。一旦API調用被啓動,它被設置爲'true'並且一旦數據被提取或發生一些錯誤,就被設置爲'false'。現在,我想在數據被提取時顯示一個微調,並在數據被提取時顯示一個FlatList。我知道我可以通過將return語句包裝進if-else條件來做到這一點。我想應該有一些更好的方法來做到這一點。 如果有人能幫助我,請告訴我一種在React Native中實現這種有條件呈現的好方法。先謝謝你。在React Native中使用條件呈現的最佳方法
1
A
回答
1
我不這麼認爲。當調用render()
方法時,適當的組件需要是基於狀態返回的。
render() {
const isLoading = this.state.isLoading
return isLoading ?
<Spinner /> //return a spinner
:
<FlatList /> return a list with data
}
1
如果這是你到處使用有幾種方法來抽象圖案了一個模式:
創建一個通用
<Loading />
組件:class Loading extends React.Component { static defaultProps = { waitingElement: <Spinner />, renderedElement: null }; render() { return this.props.loading ? this.props.waitingElement : this.props.renderedElement; } } // In some other component's `render`: <Loading renderedElement={<component with=props />}, loading={this.state.isWaiting} />
使用higher-訂購組件來包裝您的組件:
function withLoading(Component, spinner = <Spinner />) { return class extends Component { render() { if (this.props.loading) return spinner; return super.render(); } }; } // Some other file completely export default withLoading(class MyComponent { render() { return "Happy path only!"; } });
相關問題
- 1. react-native:條件呈現不起作用
- 2. React,在組件事件上呈現消息的最佳方法
- 3. React Native - 有條件地呈現視圖
- 4. React Native:如何使用耦合條件呈現大量條件視圖?
- 5. React Native TouchableHighlight呈現爲空
- 6. ListView行不在React Native中呈現
- 7. ListView呈現React Native中的空行0.39.2
- 8. React Native中的預呈現ListView
- 9. React,Mobx,Firebase條件呈現
- 10. iOS的react-native-camera無法使用最新的react-native v0.49
- 11. React Native條件呈現臨時顯示內容
- 12. 使用Redux連接條件呈現ReactJs中的子元素的最佳做法?
- 13. React中HTML元素的條件呈現?
- 14. 如何使用react-native在Android中呈現SVG?
- 15. 在原生android視圖(UI組件)中呈現react-native組件
- 16. React native-創建單例模式的最佳方法
- 17. 如何使用redux在react-native-router-flux中實現react-native-drawer?
- 18. React Native呈現相同的路由
- 19. React Native UI組件方法
- 20. RSS無法在React組件中呈現
- 21. 使用React Native進行Web抓取和注入JavaScript的最佳方法?
- 22. 調用子組件的方法 - React Native
- 23. 我在使用react-native-git-upgrade升級到react-native 0.45後出現「無法找到條目文件index.android.js」錯誤
- 24. 最佳方法 - 視圖中的條件
- 25. ViewPagerAndroid不使用React-Native在ScrollView下呈現
- 26. 如何react-native呈現可變視圖?
- 27. 在React Native中使用TextInput等組件進行DRY的最佳實踐?
- 28. React Native:我可以單獨呈現列表中的組件嗎?
- 29. 在呈現方法中渲染React具有承諾的組件
- 30. 使用現有標記呈現Zend_Form的最佳方式