javascript - React Native,值没有更新
问题描述
我制作了一个编辑屏幕并尝试使用 getParams 并设置 setParams 通过导航 v4 更新帖子的值,但是当我更改旧值并单击保存按钮以保存它时,它没有更新它,也没有显示错误。它仍然显示旧值。有人可以帮我吗,下面是我的代码
EditScreen.js
class EditScreen extends Component {
render() {
const { params } = this.props.navigation.state;
return (
<KeyboardAvoidingView
behavior="position"
keyboardVerticalOffset={Platform.OS === "ios" ? 0 : 100}
>
<Image
style={styles.image}
source={this.props.navigation.getParam("image")}
/>
<View style={styles.detailContainer}>
<AppTextInput
value={this.props.navigation.getParam("title")}
onChangeText={(text) =>
this.props.navigation.setParams({ title: text })
}
/>
<AppTextInput
value={this.props.navigation.getParam("des")}
onChangeText={(text) =>
this.props.navigation.setParams({ des: text })
}
/>
</View>
<AppButton
text="Save"
style={styles.button}
onPress={() => {
this.props.navigation.getParam("onEdit");
this.props.navigation.goBack();
}}
/>
</KeyboardAvoidingView>
主页.js
class Home extends Component {
state = {
modal: false,
post: [
{
key: "1",
title: "A Good Boi",
des: "He's a good boi and every one know it.",
image: require("../assets/dog.jpg"),
},
{
key: "2",
title: "John Cena",
des: "As you can see, You can't see me!",
image: require("../assets/cena.jpg"),
},
],
};
onEdit = (data) => {
const newPosts = this.state.post.map((item) => {
if (item.key === data.key) return data;
else return item;
});
this.setState({ post: newPosts, editMode: false });
};
render() {
return (
<Screen style={styles.screen}>
<FlatList
data={this.state.post}
renderItem={({ item }) => (
<>
<TouchableOpacity
activeOpacity={0.7}
onPress={() =>
this.props.navigation.navigate("Edit", {
image: item.image,
title: item.title,
des: item.des,
onEdit: this.onEdit,
})
}
style={styles.Edit}
>
<MaterialCommunityIcons
name="playlist-edit"
color="green"
size={35}
/>
</TouchableOpacity>
<Card onPress={() => this.props.navigation.push("Details", item)}>
<Image style={styles.image} source={item.image} />
<View style={styles.detailContainer}>
<Text style={styles.title} numberOfLines={1}>
{item.title}
</Text>
<Text style={styles.subTitle} numberOfLines={2}>
{item.des}
</Text>
</View>
</Card>
</>
解决方案
我建议您将数据保持在组件状态:
constructor(props) {
super(props);
// get the data that you need from navigation params
const { key, title, ..} = this.props.navigation.state.params
this.state = {key, title, ..}
}
然后 :
<AppTextInput
value={this.state.title}
onChangeText={(text) =>
this.setState({ title: text })
}
/>
<AppButton
text="Save"
style={styles.button}
onPress={() => {
this.props.navigation.getParam("onEdit")(this.state);
this.props.navigation.goBack();
}}
/>
推荐阅读
- react-native - 反应导航嵌套抽屉项目
- flutter - 有没有一种快速的方法可以在我的颤振应用程序中使用带有选择语言选项的欢迎屏幕?
- arduino - ENC28j60 Arduino 不显示小数
- numpy - 如何有效地读取文件夹中的所有图像?
- swiftui - SwiftUI - 使用可扩展视图构建 LazyVGrid
- android - 未在 Kotlin 应用程序中创建 SQLite 数据库
- javascript - “share_target”在我的 PWA 应用程序中不起作用
- c++ - 找到最接近其质心的几何内部点
- python - 为什么我不能将图像上传到在 Ubuntu 服务器上运行的 Django 应用程序?
- google-app-engine - 您如何通过 GCP 应用引擎为网站提供服务?