2016-11-10 70 views
3

我想知道是否有可能将参数传递给反应入口点。将值传递给捆绑的React JS文件?

我的切入点是这样的:

module.exports = { 
    entry: "./js/components/Application.js", 
    output: { 
     path: "./dist", 
     filename: "bundle.js" 
    }, 
    // ... 
} 

我的application.js:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import AnotherComponent from './AnotherComponent'; 

ReactDOM.render(<AnotherComponent />, document.getElementById('content')); 

不是我捆绑我用的WebPack应用和包括这种捆绑在其它应用程序。此应用程序提供ID为“内容”一个div:

<body> 
    <div id="content"></div> 
    <script src="bundle.js" /> 
</body> 

我知道我可以这样做

<script src="bundle.js" myargument="somevalue" /> 

,但我怎么能得到这个值,将它传递给反应成分AnotherComponent作为属性?

回答

2

什么

<script id="bundle" src="bundle.js" myargument="somevalue" /> 

然后

const myScript = document.getElementById('bundle'); 

ReactDOM.render(<AnotherComponent 
    myArgument = {myScript.getAttribute('myargument')} 
/>, document.getElementById('content') 

); 
+0

这个工作,非常感谢! (我不认为我实际上可以获得'脚本',只是从中获得价值)。 – Ria

+0

不客气;-) – lustoykov