gpt4 book ai didi

javascript - 如何在 React Native 中使用 setNativeProps 更改图片来源

转载 作者:行者123 更新时间:2023-11-29 16:40:00 27 4
gpt4 key购买 nike

我正在用 React Native 开发 UI,在此 <Image>与源一起使用,我想通过 setNativeProps 而不是通过状态更改图像源。

我的代码是:

<Image source={require('imagePath')} ref="icon"/>

在某些事件上我想改变它的来源:

this.refs['icon'].setNativeProps({
source: require('newImagePath')
});

但是什么也没有发生,它没有改变。通过使用 NativeProps,图像的样式发生了变化,但 source 之类的 Prop 没有改变。

所以请给我一个解决方案,我如何通过使用它的 refs 或键(而不是使用 this.setState)来更改任何特定事件的图像源

最佳答案

最后我找到了可以通过静态图像更改图像源的方法。我们首先需要对静态图片使用resolveAssetSource函数,然后将其放入src中。

import resolveAssetSource from 'resolveAssetSource';

...


var imgsrc = require('imagePath');

this.refs['icon'].setNativeProps({
src: [resolveAssetSource(imgsrc)]
});

更新:

在 IOS 上,使用 source 而不是 src。

关于javascript - 如何在 React Native 中使用 setNativeProps 更改图片来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47234873/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com