簡體   English   中英

狀態更改時,使用狀態的自定義組件不會更新

[英]Custom component that makes use of state doesn't update when state changes

我目前正在使用require在構造函數中設置默認圖片源:

constructor(props) {
super(props);
    this.state = {
       name: '',
       pictureSource: require('../assets/images/robot-dev.png')
    };
}

我的自定義組件正在使用pictureSource作為道具,而我的TextInput正在使用名稱:

<EditableRoundedImage picture={this.state.pictureSource}/>
<Text style={styles.name}>{this.state.name}</Text>

---Inside EditableRoundedImage---

constructor(props) {
   super(props);
   this.state = {
     image: this.props.picture
   };
}
<Image style={styles.roundedImage} source={this.state.image} />

在componentDidMount生命周期方法中,im會獲取一些數據並隨后更改pictureSource / name:

this.setState({
      name: jsonResponse.firstName + " " + jsonResponse.lastName,
      pictureSource: {uri: jsonResponse.imageUrl}
    })

* jsonResponse.imageUrl具有以下內容: https ://ph-files.imgix.net/b5541240-7da7-4bf0-8dc2-c9e911b283f2?auto=format&auto=compress&codec=mozjpeg&cs=strip

由於某種原因,新獲取的圖片未顯示在應用程序中,但名稱顯示了。 我想知道它是否與{uri:}表示法有關,或者我是否正在監督某些事項。

提前致謝!

您正在將this.state.image分配給圖像源。 但您知道狀態沒有稱為image的屬性。 這可能是問題嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM