What is the correct syntax? This approach inserts the text of the html into the client’s browser, i.e. <div>===STAR===</div><div>===STAR===</div><div>===STAR===</div> appears as a string in the browser, rather being parsed into elements and inserted into the dom:
import React from 'react';
function displayStarRating(starRating){
var result = '';
for (var i = 1; i <= starRating; i++){
result += '<div>===STAR===</div>';
}
return result;
}
class Reviews extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{displayStarRating(4)}
</div>
);
}
}
export default Reviews;
I also tried this approach:
import React from 'react';
function displayStarRating(starRating){
var result = '';
for (var i = 1; i <= starRating; i++){
<div>===STAR===</div>
}
}
class Reviews extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{displayStarRating(4)}
</div>
);
}
}
export default Reviews;
…but the div and its text “===STAR===” do not appear in the resulting html source or in the client browser.
function displayStarRating(starRating) {
var result = [];
for (var i = 1; i <= starRating; i++) {
result.push(<div>===STAR===</div>);
}
return result;
}
Maybe, but this was also the simplest refactor of @vikr00001’s code. The error in that code was that React elements in the JSX code were being treated as template strings, so the only change I made was to correct the data type.
I also remain unconvinced that creating an array for the sole purpose of having something to map over is a better solution. It feels like an imperative solution masquerading as a declarative one to me. I did like the idea of mapping over the integer. That sent me straight to the repl hoping that it was a thing!