I have a React app like this.
var X = React.createClass({
componentDidMount: function() {
fetch(this.props.feed).then(...);
}
render: function() {
return <div>{this.props.feed}</div>
}
});
The feed prop is used to get a JSON feed in componentDidMount that's unique for a particular customer.
It would be convenient to pass data into my React app from the HTML to parameterise it:
<html>
<body>
<div id="app" feed='custom_feed.json'></div>
</body>
</html
My current solution looks like this:
var root = document.getElementById('app');
var feed = root.getAttribute('feed')
ReactDOM.render(<X feed={feed}/>, root);
This obviously works, but it feels like there ought to be a more idiomatic solution. Is there a more React way to do this?
question from:
https://stackoverflow.com/questions/36545118/is-there-a-right-way-to-pass-data-into-a-react-component-from-the-html-page 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…