如何在React Native中动态包含模块

问题描述 投票:2回答:2

我正在尝试动态地进行动态导入import (this.props.myImportModulePath)但我得到了:

Error: TrasformError index.js: index.js:Invalid call at line 12: import(_this.props.myImportModulePath), js engine: hermes  

我不确定这是解决问题的方法,但是我试图获得的是动态获得import ToastExample from './ToastExample';

下面是我的处理方法。

谢谢大家。

import React, {Component} from 'react';
import {AppRegistry, Text} from 'react-native';

import {name as appName} from './app.json';

class HelloWorld extends Component {

    constructor(props) {
        super(props);

        this.state = {
            OtherComponent: React.lazy(() => import (this.props.myImportModulePath))
        }
    }

    render() {
        return (
            <Text>HELLO World</Text>
        );
    }
}
AppRegistry.registerComponent(appName, () => HelloWorld);  

请注意,当我更改时,此方法有效

OtherComponent: React.lazy(() => import (this.props.myImportModulePath))

进入

OtherComponent: React.lazy(() => import ('./myImportModule'))

即,将路径直接作为字符串'./myImportModule'传递,而不是将其作为propthis.props.myImportModulePath传递

谢谢大家。

javascript android reactjs react-native
2个回答
2
投票

我认为您不能动态使用导入,但是可以动态地更改要调用的组件,语法如本文所提及:React - Dynamically Import Components


0
投票

我认为您想根据某些条件导入某些组件。我认为您可以按照以下步骤进行操作:

© www.soinside.com 2019 - 2024. All rights reserved.