简体   繁体   English

状态更改时,使用状态的自定义组件不会更新

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

I'm currently setting a default picture source in the constructor with require: 我目前正在使用require在构造函数中设置默认图片源:

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

My custom component is making use of the pictureSource as a prop and my TextInput is making use of name: 我的自定义组件正在使用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} />

In the componentDidMount lifecycle method im fetching some data and changing the pictureSource/name afterwards: 在componentDidMount生命周期方法中,im会获取一些数据并随后更改pictureSource / name:

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

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

For some reason the newly fetched picture isn't displaying in the app, but the name is. 由于某种原因,新获取的图片未显示在应用程序中,但名称显示了。 I was wondering if it had something to do with the {uri: } notation or if i'm overseeing something. 我想知道它是否与{uri:}表示法有关,或者我是否正在监督某些事项。

Thanks in advance! 提前致谢!

you are assigning this.state.image to the image source. 您正在将this.state.image分配给图像源。 But you are state doesn't have a property called image . 但您知道状态没有称为image的属性。 Could this be the problem? 这可能是问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM