亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號