function - react-native 中的小问题 活动指标 | 组件 | 组件内的道具 | 道具 | 设置状态
问题描述
所以我的问题是我需要在函数/组件中设置一个状态。默认情况下,状态“isLoading”设置为true(它用于 ActivityIndicator),我需要在组件内将其更改回false,以便指示器停止工作并且组件呈现结果。
这是代码:
const Data = require('../data/my_data.json');
export default class Albums extends React.Component {
constructor(props) {
super(props);
this.state = {
Data_list : Data,
isLoading: true,
};
componentWillMount() {
return this.state.Data_list.map(something=> (
<list_Detail key={something.id} something={something} />
));
}
render() {
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return (
<ScrollView>{this.componentWillMount()}</ScrollView>
)}
}
我已经尝试过了:
componentWillMount() {
return this.state.Data_list.map(something=> (
<list_Detail key={something.id} something={something} />
))
.then(this.setState({isLoading: false}));
}
但它没有用
所以任何想法!!!!????
解决方案
componentWillMount
是一个生命周期方法,在组件渲染之前调用。您不能从此方法返回 UI
将 UI 部分移至render
方法并仅保留 api 调用componentWillMount
。
componentWillMount() {
axios.get('https://api.jsonbin.io/b/5d05c8712132b7426d0')
.then(response => this.setState({Data: response.data, isLoading: false}));
));
}
在render
方法上,
render(){
return (
//other UI
{this.state.Data.map(something => (
<list_Detail key={something.id} something={something} />
/>
))}
}
在此处查找 forcomponentWillMount
和其他生命周期方法的用法
推荐阅读
- package - 还原期间锁定依赖项生成的 nuget packages.lock.json 文件的架构
- angular - 如何处理迭代Angular html中的子类型数组?
- sql - result_fetch(res@ptr, n) 中的错误:nanodbc/nanodbc.cpp:2695:24000:[Microsoft][ODBC SQL Server Driver]游标状态无效
- python - 在熊猫图中显示第一个和最后一个标签
- python - VS Code .py ModuelNotFoundError 没有名为“pyodbc”的模块
- java - 如何在 Android Studio 中自动增加 TextView 的值
- c# - ASP NET MVC - 默认情况下全局禁用 OutputCache
- linux - 如何获取 flac 和 ogg 的音频文件元数据容器的数据
- node.js - 如何获得正确的 NodeJS Child Process.spawn 输出
- javascript - 使用反应返回到分页中的当前页面