简体   繁体   English

使用 Expo/React Native 和 Next 未显示图像

[英]Images not showing with Expo/React Native and Next

So I am trying to use the Native Base Solito starter:所以我正在尝试使用 Native Base Solito 启动器:

https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript

This is the first time I've tried to work with Next, and I am trying to get image support with Expo.这是我第一次尝试使用 Next,我正在尝试使用 Expo 获得图像支持。

Per the Expo documentation:根据 Expo 文档:

https://docs.expo.dev/guides/using-nextjs/ https://docs.expo.dev/guides/using-nextjs/

I should be able to just use next-image , which I am doing:我应该能够使用我正在做next-image

const { withNativebase } = require('@native-base/next-adapter')
const withImages = require('next-images')

module.exports = withNativebase(
  withImages({
    dependencies: [
      '@expo/next-adapter',
      'next-images',
      'react-native-vector-icons',
      'react-native-vector-icons-for-web',
      'solito',
      'app',
    ],
    nextConfig: {
      projectRoot: __dirname,
      reactStrictMode: true,
      webpack5: true,
      webpack: (config, options) => {
        config.resolve.alias = {
          ...(config.resolve.alias || {}),
          'react-native$': 'react-native-web',
          '@expo/vector-icons': 'react-native-vector-icons',
        }
        config.resolve.extensions = [
          '.web.js',
          '.web.ts',
          '.web.tsx',
          ...config.resolve.extensions,
        ]
        return config
      },
    },
  })
)

Despite this, my images are just not displaying in Next.尽管如此,我的图像只是没有显示在 Next 中。 Elements are generated with the styling I am applying to the image elements, but the images themselves are not displaying.元素是使用我应用于图像元素的样式生成的,但图像本身没有显示。

I tried both universal routing import and direct path:我尝试了通用路由导入和直接路径:

import GrayBox from 'resources/images/graybox.png'
import Car from '../../../../packages/app/resources/images/car.png'

As well as several different images uses:以及几种不同的图像用途:

<Image
  source={require('../../../../packages/app/resources/images/car.png')}
  style={{ width: 500, height: 750 }}
  alt="test"
/>

<Image
  source={GrayBox}
  key={index}
  style={{ width: 500, height: 750 }}
  alt="test2"
/>

<Image
  source={Car}
  key={index}
  style={{ width: 500, height: 750 }}
  alt="test3"
/>

None of these images are displayed.这些图像均未显示。

I've tried both the react native image:我已经尝试过反应原生图像:

https://reactnative.dev/docs/image https://reactnative.dev/docs/image

As well as the native base wrapped one.以及原生基地包装之一。

Still nothing.依然没有。

Any clue what is wrong in my configuration to cause images to not show?任何线索我的配置中有什么问题导致图像不显示?

I suspect it's something in my next.config.js我怀疑它在我的next.config.js

EDIT:编辑:

If I add:如果我添加:

  plugins: [withImages, withFonts, [withExpo, { projectRoot: __dirname }]],

to my next.config.js, I get the following error:到我的 next.config.js,我收到以下错误:

../../packages/app/resources/images/cart_black.png
TypeError: unsupported file type: undefined (file: undefined)

This seems to solve my problem, if I add it to my next.config.js file:如果我将它添加到我的 next.config.js 文件中,这似乎解决了我的问题:

plugins: [withImages, [withExpo, { projectRoot: __dirname }]],
  nextConfig: {
    images: {
      disableStaticImages: true,
    },
    ...
    }

I need more information (that I have mentioned as comment) to say the exact solution but I guess two things:我需要更多信息(我在评论中提到过)来说出确切的解决方案,但我猜有两件事:

  1. You have used wrong prop to pass the image to the <Image component您使用了错误的道具将图像传递给<Image组件

    import SomeThingPng from '[/someWhere/something.png]'; <Image src={SomeThingPng} // <= Don't use source

    Instead of using source use src而不是使用source使用src

  2. My second guess is your assets path loading that you have configured in the:我的第二个猜测是您在以下位置配置的资产路径加载:

     nextConfig: { projectRoot: __dirname,

    Maybe you should some additional name to __dirname or use basePath for changing your publicPath of Webpack to correctly load your images in the browser with the correct image address.也许您应该为__dirname一些额外的名称,或者使用basePath更改您的Webpack的 publicPath,以便使用正确的图像地址在浏览器中正确加载图像。 actually, my second guess is Wrong image path address实际上,我的第二个猜测是错误的图像路径地址

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

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