javascript - React Native FlatList: Toggle State Value
问题描述
I am trying toggle text state from ACTIVE
to INACTIVE
(and vice versa) for each individual item in the FlatList. In the code below, the status
toggles from true
to false
(and false
to true
) but the text in the app shows inactive
and doesn't change.
import NameActionBar from '../components/NameActionBar';
export default class MasterScreen extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: [],
status: false,
};
}
componentDidMount() {
this.getFreelancerList();
}
//Calling API to get list
getFreelancerList() {
let self = this;
AsyncStorage.getItem('my_token').then((keyValue) => {
console.log('Master Screen (keyValue): ', keyValue); //Display key value
axios({
method: 'get',
url: Constants.API_URL + 'user_m/freelancer_list/',
responseType: 'json',
headers: {
'X-API-KEY': Constants.API_KEY,
'Authorization': keyValue,
},
})
.then(function (response) {
console.log('Response.Data: ===> ', response.data.data);
console.log('Response: ', response);
self.setState({
dataSource: response.data.data,
});
})
.catch(function (error) {
console.log('Error: ', error);
});
}, (error) => {
console.log('error error!', error) //Display error
});
}
//Show the list using FlatList
viewFreelancerList() {
const { dataSource } = this.state;
return (
<View>
{<FlatList
data={dataSource}
keyExtractor={({ id }, index) => index.toString()}
renderItem={({ item }) => {
return (
<View style={styles.containerFreelancer}>
<TouchableOpacity
style={{ flex: 1 }}
onPress={() => console.log(item.freelancer_name)}
>
<Text style={styles.textFreelancer}>{item.freelancer_name}</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
const newStatus = !this.state.status;
this.setState({
status: newStatus,
});
console.log('status: ', this.state.status);
}}
>
<Text>{this.state.status ? "ACTIVE" : "INACTIVE"}</Text>
</TouchableOpacity>
</View>
);
}}
/>}
</View>
);
}
render() {
return (
<>
<NameActionBar />
<ScrollView>
{this.viewFreelancerList()}
</ScrollView>
</>
);
}
}
My issues are:
How can I make the text toggle between
active
toinactive
?How can I make the text toggle separately for each item in the FlatList? for example:
Item 1: 'ACTIVE', Item 2: 'INACTIVE'
etc.
Any help would be appreciated as I am still new to learning React Native.
Screenshot of the app below:
解决方案
您需要创建一个具有自己状态的子组件。
class FlatListComponent extends Component {
state = {
status: false
}
render() {
<View style={styles.containerFreelancer}>
<TouchableOpacity style={{ flex: 1 }} onPress={() => console.log(this.props.freelancer_name)}>
<Text style={styles.textFreelancer}>{this.props.freelancer_name}</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => {
const newStatus = !this.state.status;
this.setState({
status: newStatus,
});
console.log('status: ', this.state.status);
}}
>
<Text>{this.state.status ? "ACTIVE" : "INACTIVE"}</Text>
</TouchableOpacity>
</View>
}
}
然后你只需要将它添加到你的renderItem
方法中。
<FlatList
data={dataSource}
keyExtractor={({ id }, index) => index.toString()}
renderItem={({ item }) => <FlatListComponent {...item}/>
/>}
这是一个工作示例
我希望它有帮助!如果您仍然卡住,请随时添加评论
推荐阅读
- javascript - 对象的 Javascript CRUD 操作。如何对响应对象进行 CRUD 操作
- c++ - 获取内部嵌套向量的`value_type`
- raku - 可以使用聚集来展开路口吗?
- go - 为什么逐行读取文件需要更多内存?
- maven - 引用未定义的变量surefireArgLine - 无法运行硒文件
- amazon-web-services - 是什么导致 AWS Lambda 上出现此 Runtime.ExitError?
- c# - 测试 .NET Framework 时,在 .NET Core xUnit 测试项目中发现无法加载类型“System.Web.HttpContextBase”错误
- reactjs - 如何更改样式组件在反应中生成的类?
- python-3.x - 未找到模块错误:没有名为“squarify”的模块
- ssl - 如何使用 SSL 保护 Spyne Web 服务?