error related to images in react native app

  Kiến thức lập trình
import React, { useState } from 'react';
import type { PropsWithChildren } from 'react';

import { ImageSourcePropType, StyleSheet, View, Image, Pressable, Text } from 'react-native';

import DiceOne from '../assets/one.png'
import DiceTwo from '../assets/two.png'
import DiceThree from '../assets/three.png'
import DiceFour from '../assets/four.png'
import DiceFive from '../assets/five.png'
import DiceSix from '../assets/six.png'



type DiceProps = PropsWithChildren<{
  imageUrl: ImageSourcePropType
}>

const Dice = ({ imageUrl }: DiceProps): JSX.Element => {
  return (
    <View>
      <Image style={styles.diceImage} source={imageUrl} />
    </View>
  )
}






function App(): React.JSX.Element {

  const [diceImage, setDiceImage] = useState<ImageSourcePropType>(DiceOne)

  const rollDiceontap = () => {
    let randomNumber = Math.floor(Math.random() * 6) + 1;

    switch (randomNumber) {
      case 1:
        setDiceImage(DiceOne)
        break;
      case 2:
        setDiceImage(DiceTwo)
        break;
      case 3:
        setDiceImage(DiceThree)
        break;
      case 4:
        setDiceImage(DiceFour)
        break;
      case 5:
        setDiceImage(DiceFive)
        break;
      case 6:
        setDiceImage(DiceSix)
        break;


      default:
        setDiceImage(DiceOne)
        break;

    }
  }

  return (
    <View style={styles.container}>
      <Dice imageUrl={diceImage} />
      <Pressable onPress={rollDiceontap}>
        <Text style={styles.rollDiceBtnText}>
        Roll the dice
        </Text>
        
      </Pressable>
    </View>

  );
}

i am creating simple dice roll app on react native, the codes seems to be perfect and i have also create the module file also index.d.ts but i am getting this error, mentioned below, so please help as i am new to reactnative

error: Error: Unable to resolve module ../assets/one.png from C:UsersAdityaDesktopNew folderrolldiceApp.tsx:

None of these files exist:

  • one.png
  • ..assetsone.pngindex(.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)
  11 | import { ImageSourcePropType, StyleSheet, View, Image, Pressable, Text } from 'react-native';
  12 |
> 13 | import DiceOne from '../assets/one.png'
     |                      ^
  14 | import DiceTwo from '../assets/two.png'
  15 | import DiceThree from '../assets/three.png'
  16 | import DiceFour from '../assets/four.png'"

LEAVE A COMMENT