Is there a way of having an undefined string passed to an Image in react native

huangapple go评论76阅读模式
英文:

Is there a way of having an undefined string passed to an Image in react native

问题

我目前有这段代码:

  const renderImage = (imageUrl: string) => {
    if (imageUrl)
      return (<Image style={styles.image} source={require(imageUrl)} />);
    return (<View></View>)
  }

我收到的错误是: "在第28行发生无效调用: require(imageUrl)"。我原以为 "if(imageUrl)" 会解决这个问题。

我尝试过,但图片没有被渲染:

  const renderImage = (imageUrl: string) => {
    if (imageUrl)
      return (<Image style={styles.image} source={{ uri: imageUrl }} />);
    return (<View></View>)
  }

编辑: 在加载数据时,imageUrl 将为 null,这是导致错误的原因。

英文:

I currently have this bit of code :

  const renderImage = (imageUrl: string) => {
    if (imageUrl)
      return (<Image style={styles.image} source={require(imageUrl)} />);
    return (<View></View>)
  }

and the error I am getting is : "Invalid call at line 28: require(imageUrl)". I would have thought that the "if(imageUrl)" would have sorted that out.

I have tried but the image is not being rendered:

  const renderImage = (imageUrl: string) => {
    if (imageUrl)
      return (<Image style={styles.image} source={{ uri: imageUrl }} />);
    return (<View></View>)
  }

EDIT: The imageUrl will be null while loading data which is what is causing the error.

答案1

得分: 2

确保您从react native中导入Image并为宽度和高度添加样式:

<Image style={{ width: 100, height: 100 }} source={{ uri: "https://i.stack.imgur.com/RWR5C.jpg?s=256&g=1" }} />;

示例

import React from "react";
import { Image, ImageRequireSource, ImageURISource, View } from "react-native";

const imageURI = "https://i.stack.imgur.com/RWR5C.jpg?s=256&g=1";

const assetImage = require("./assets/images/icon.png");

const RenderURIImage = ({ imageURI }: { imageURI: ImageURISource["uri"] }) => {
  return (
    <Image source={{ uri: imageURI }} style={{ width: 100, height: 100 }} />
  );
};

const RenderAssetImage = ({ image }: { image: ImageRequireSource }) => {
  return <Image source={image} style={{ width: 100, height: 100 }} />;
};

function App() {
  return (
    <View style={{ flex: 1 }}>
      <RenderURIImage imageURI={imageURI} />

      <RenderAssetImage image={assetImage} />
    </View>
  );
}

请注意,这是您提供的代码的翻译部分。

英文:

make sure you import Image from react native and add styles for width and height:

&lt;Image style={{ width: 100, height: 100 }}  source={{uri : &quot;https://i.stack.imgur.com/RWR5C.jpg?s=256&amp;g=1&quot;}} /&gt;;

Example

import React from &quot;react&quot;;
import { Image, ImageRequireSource, ImageURISource, View } from &quot;react-native&quot;;


const imageURI = &quot;https://i.stack.imgur.com/RWR5C.jpg?s=256&amp;g=1&quot;;

const assetImage = require(&quot;./assets/images/icon.png&quot;);

const RenderURIImage = ({ imageURI }: { imageURI: ImageURISource[&quot;uri&quot;] }) =&gt; {
  return (
    &lt;Image source={{ uri: imageURI }} style={{ width: 100, height: 100 }} /&gt;
  );
};

const RenderAssetImage = ({ image }: { image: ImageRequireSource }) =&gt; {
  return &lt;Image source={image} style={{ width: 100, height: 100 }} /&gt;;
};

function App() {
  return (
    &lt;View style={{ flex: 1 }}&gt;
      &lt;RenderURIImage imageURI={imageURI} /&gt;

      &lt;RenderAssetImage image={assetImage} /&gt;
    &lt;/View&gt;
  );
}

答案2

得分: 0

请确保您在图像上传递了尺寸,因为图像需要尺寸才能渲染,否则它永远不会渲染。您可以尝试像这样做:

<Image style={{ width: 200, height: 200 }} source={{ uri: imgUrl }} />;

还可以添加默认图像:

<Image style={{ width: 200, height: 200 }} source={{ uri: imgUrl || defaultImg }} />;

这样您可以确认发生了什么情况。希望它能正常工作。

英文:

Please make sure you are passing size to image because image need size to render otherwise it will never render. You can try something like this.

&lt;Image style={{ width: 200, height: 200 }}  source={{uri : imgUrl}} /&gt;;

And also you can add default image.

&lt;Image style={{ width: 200, height: 200 }}  source={{uri : imgUrl || defaultImg }} /&gt;;

So you can confirm it what's going on. I hope it will work.

huangapple
  • 本文由 发表于 2023年6月22日 15:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定