反应本机无法动态更改图像组件源uri

时间:2020-03-25 15:15:59

标签: react-native

我遇到了带有本机响应(0.61.5)的奇怪问题/错误。我有一个屏幕,用户可以在其中更改他的个人资料图片。图片是从redux获取的。

const [pp, setPp] = useState(useSelector(state => state.profile.pp))

const ppChangeHandler = async () => {
    try {
        req = await fetch(...)
        res = await req.json()
        // res.url contains the url to the new uploaded image on the server
        setPp(res.url)
    } catch (e) {
        console.log(e)
    }
}

如您所见,我向服务器发送了一个包含新图像的请求(此请求未包含在代码中,但可以使用),作为响应,我收到了新生成的图像URL。收到图像网址后,我想将屏幕上的个人资料图片更新为新图像。但这是行不通的。图片消失了,没有更新。

我认为我正在遇到以下错误:Image Component will not update correctly when passing in new url #9195

我尝试了以下操作:

  • 在包含“ pp”的内容中添加“关键”道具:<Image key={pp} source={{ uri: pp }} style={{ width: 100, height: 100 }} />
  • 在源<Image source={{ uri: pp, cache: reload }} style={{ width: 100, height: 100 }} />中添加“缓存:重新加载”

有人针对此问题有提示/解决方法吗?图片的“更新”有效,当我刷新应用程序时,会显示新的个人资料图片,但我希望它立即显示,而不是在刷新应用程序后显示。

编辑:我也使用FastImage(https://github.com/DylanVann/react-native-fast-image)进行了尝试,但是得到了相同的结果。

0 个答案:

没有答案