为了账号安全,请及时绑定邮箱和手机立即绑定

React Native,值没有更新

React Native,值没有更新

互换的青春 2023-04-27 10:30:28
我制作了一个编辑屏幕,并尝试使用 getParams 和设置 setParams 通过导航 v4 更新帖子的值,但是当我更改旧值并单击保存按钮保存它时,它没有更新它,也没有显示任何错误。它仍然显示旧值。有人可以帮我吗,下面是我的代码EditScreen.jsclass 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>首页.jsclass 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 });  };
查看完整描述

2 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

我建议您将数据保持在组件状态:


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();

      }}

    />


查看完整回答
反对 回复 2023-04-27
?
慕斯王

TA贡献1864条经验 获得超2个赞

也许试试这个:


<AppButton

    text="Save"

    style={styles.button}

    onPress={() => {

        this.props.navigation.getParam("onEdit")(this.props.navigation.state.params);

        this.props.navigation.goBack();

    }}

/>

和:


this.props.navigation.navigate("Edit", {

                    key: item.key,

                    image: item.image,

                    title: item.title,

                    des: item.des,

                    onEdit: this.onEdit,

                  })


查看完整回答
反对 回复 2023-04-27
  • 2 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信