React Native 的动态导入路径

问题描述 投票:0回答:1

我想做的是导入 React Native 动态路径。

示例:- 从

./screens/${path}
;

导入演示

目前我找到了一种使用懒惰的方法。但它也只有在像这样初始化常量变量时才有效

  const demo = 'Path';

  let componentPath = `../Screen/${demo}`;

  const DynamicComponent = React.lazy(() =>
    import(componentPath).then(module => ({default: module.screenm})),
  );

我想通过异步存储保存的变量获取路径。不像硬编码变量。当我硬编码变量并且它起作用时。但我无法从异步存储中获取它。我试图找到方法来做到这一点。但我做不到。有什么方法可以做到吗

reactjs react-native react-redux
1个回答
0
投票
const demo = 'Path'; // Your dynamic path variable

// Construct the path to your component
let componentPath = `../Screen/${demo}`;

// Dynamically requires the component using the constructed path
const DynamicComponent = require(componentPath).default;

// Render the dynamically imported component
<DynamicComponent />
© www.soinside.com 2019 - 2024. All rights reserved.