简体   繁体   English

如何初始化和使用其他模块(WebPack和ES6)导出的ReactJS类?

[英]How do I initialize and use exported ReactJS classes from other modules (with WebPack and ES6)?

I'm using WebPack and ES6, so I can't use module.exports and have to use export default . 我正在使用WebPack和ES6,因此无法使用module.exports而必须使用export default I have a library like this: 我有一个像这样的图书馆:

// helloworld.js
import React from 'react';

class HelloWorld extends React.Component {
        test() {
                alert("TEST FROM test()");
        }   
        render() {
                alert("TEST");
                return (
                        <p>Hello, world!</p>
                );  
        }   
}

// THIS WORKS WHEN I REQUIRE THIS MODULE
// var thing = new HelloWorld();
// thing.test();

export default HelloWorld;

The commented out portions work when I var helloworld = require('helloworld.js'); 当我var helloworld = require('helloworld.js');时,注释掉的部分起作用 , but I can't figure out how to initialize and use this object outside where I require this. ,但我无法弄清楚如何在需要的地方初始化和使用该对象。

None of these attempts work. 这些尝试均无效。 How do I initialize this object and use it? 如何初始化该对象并使用它?

// hello_world.test();
// hello_world.HelloWorld.test();
// var thing = new hello_world();
// var thing = new hello_world.HelloWorld();

My main reason is because I need the component in a route using ReactRouter like this and none of these attempts work. 我的主要原因是因为我需要使用ReactRouter这样的路由中的组件,而这些尝试均无效。

I tried this (bellow) and this tells me to check the render method... 我尝试过这个(波纹管),这告诉我检查render方法...

ReactDOM.render(
(<BrowserRouter>
        <Switch>
                <Route exact path="/helloworld" component={hello_world}/>
        </Switch>
</BrowserRouter>)

This (bellow) renders a BLANK PAGE!!!! 这个(波纹管)呈现空白页面!

ReactDOM.render(
(<BrowserRouter>
        <Switch>
                <Route exact path="/helloworld" component={hello_world.HelloWorld}/>
        </Switch>
</BrowserRouter>)

I'm out of ideas. 我没主意了。 This does not help. 无济于事。 Neither does this . 无论是做这个 Could please someone lend me some pointers? 可以请别人借给我一些指示吗?

EDIT: 编辑:

Solution was simply to add default at the end of require ( var hello_world = require('./helloworld.js').default; ). 解决方案只是在require的末尾添加defaultvar hello_world = require('./helloworld.js').default; )。 This works to use this in the route like: <Route exact path="/helloworld" component={hello_world}/> . 这可以在如下路径中使用它: <Route exact path="/helloworld" component={hello_world}/>

If you were to use this outside the scope, you would do: 如果要在范围之外使用此功能,则可以执行以下操作:

var thing = new hello_world();
thing.test();

Working solution: 工作解决方案:

var hello_world = require('./hello_world.js').default; // Must add default.

// Using it outside a route, with a class method called test().
var thing = new hello_world();
thing.test();

// Using it in a router (ReactRouter with Switch).
ReactDOM.render(
(<BrowserRouter>
        <Switch>
                <Route exact path="/helloworld" component={hello_world}/>
        </Switch>
</BrowserRouter>)
, document.getElementById("root"));

You should be able to use the component in a regular jsx file like this: 您应该能够在常规jsx文件中使用该组件,如下所示:

var HelloWorld = require('path/to/HelloWorld.jsx');
...    
<div>
    <HelloWorld></HelloWorld>
</div>

Or in a react-router component, with the same require statement like this: 或在react-router组件中,使用相同的require语句,如下所示:

var HelloWorld = require('path/to/HelloWorld.jsx');
....
<Route exact path="/helloworld" component={HelloWorld}/>

var helloworld = require('helloworld.js').default is working for me. var helloworld = require('helloworld.js').default对我有用。 Can you check this? 你能检查一下吗? Please guide me if I'm doing wrong here. 如果我在这里做错了,请指导我。

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

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