I am rendering multiple divs using the class but I see only the first div rendered.
<html>
<body>
<div class="elem" name="foo"/>
<div class="elem" name="bar"/>
<script src="public/bundle.js" type="text/javascript"></script>
</body>
</html>
JSX:
import React from 'react';
import {render} from 'react-dom';
class MyElement extends React.Component {
render () {
return <div>NAME: {this.props.name}</div>
}
}
var divs = document.getElementsByClassName('elem');
Array.prototype.forEach.call(divs, function(div, index) {
render(<MyElement name={div.getAttribute('name')}/>, div);
});
I only see "NAME: foo" but no "NAME: bar". I even tried adding "key={index}" to element but no use.
Here are the dependencies used:
"dependencies": {
"babel-core": "^6.23.1",
"babel-loader": "^6.4.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"webpack": "^2.2.1"
}
Your divs should define with start and end tags.
<div class="app" name="foo"></div>
<div class="app" name="bar"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.