将值传递给捆绑的React JS文件? [英] Passing a value to bundled React JS file?

查看:68
本文介绍了将值传递给捆绑的React JS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将参数传递给react入口点。

I wonder if it is possible to pass an argument to a react entry point.

我的入口点如下所示:

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

我的Application.js:

My Application.js:

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

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

不是我用webpack捆绑我的应用程序并将此捆绑包包含在另一个应用程序中。此应用程序提供ID为content的div:

Not I bundle my application with webpack and include this bundle in another application. This application provides a div with id "content":

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

我知道我可以做点什么

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

但如何将此值传递给react组件 AnotherComponent 作为财产?

but how can I get this value for passing it to the react component AnotherComponent as a property?

推荐答案

怎么样

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

然后

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

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

);

这篇关于将值传递给捆绑的React JS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆