Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6581

Trouble with Dynamic Imports in React Native TypeScript component

$
0
0

I'm encountering an issue with dynamic imports in a React Native app using TypeScript. I'm attempting to dynamically import JSON files based on a reference prop in one of my components, but I'm facing an error that I can't resolve.

import React, { useEffect, useState } from "react";import { View, Text } from "react-native";import styles from "./styles";type Props = {  reference: string;};const Block = ({ reference }: Props) => {  const [unit, setUnit] = useState<any[]>([]);  useEffect(() => {    const fetchData = async () => {      try {        const module = await import(`../../../assets/data/topics/${reference}`);        setUnit(module);      } catch (error) {        console.error("Error loading data:", error);      }    };    fetchData();  }, [reference]);  return (<View style={styles.container}>      {unit.map((block: any) => (<View key={block.blockId} style={styles.blockBox}><Text style={styles.boxText}>{block.blockName}</Text></View>      ))}</View>  );};export default Block;

The reference prop is just a string for the end of the path units/top-1-sec-1-unit-1.json.

The error I'm seeing is:

Invalid call at line 16: import(../../../assets/data/topics/${reference})

I've checked the path, and it seems correct. Any ideas on what might be causing this issue? I'd appreciate any help or guidance. Thank you!


Viewing all articles
Browse latest Browse all 6581

Latest Images

Trending Articles



Latest Images