V
V
Vladimir T.2018-02-21 19:50:59
React
Vladimir T., 2018-02-21 19:50:59

How to pass html attribute to reactjs app?

There is a web application which has index.hml with the following code:

<body>
    <div id="root" some-param="value"></div>
    <script src="app.js"></script>
</body>

which is customarily initialized like this: How can I read the value of some-param html attribute in the App component ?
render(<App />, document.getElementById('root'));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Aleksandrovich, 2018-02-21
@vachuahe

var x = document.getElementById("root").getAttribute("some-param");

V
Vladimir T., 2018-02-22
@vachuahe

Perhaps someone else will need almost the same option with forwarding to props:

const root = document.getElementById('root');
render(<App {...(root.dataset)} />, root);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question