3

For example, I have a react.js component named Question which need the props title,id,content.

<div id="main-section">
 <div id="question"></div>
</div>
ReactDOM.render(
 <Question id="xxxx" title="xxx" content="xxx"/>,
 document.getElementById('question')
);

I come up with two methods:

  1. design an restful api, everytime the web need to render a question, make an ajax request to the server to get the props.
  2. store the props in dom element and get these props before rendering.

What's the best practise to do such thing? is there any other better method?

asked Sep 22, 2016 at 3:44

1 Answer 1

1

You can define you element first as a const/var or whatever. Then you access the element's attributes with element.attributes and iterate through your attributes like so:

const element = document.getElementById('question');
ReactDOM.render(
 <Question {...element.attributes} />,
 element
);

You can access the attributes with this.props

If you're passing data-* attributes your can pass those with {...element.dataset}

answered May 4, 2020 at 18:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.