[英]I want to repeat “★” as many times as a number in state in React
I want to repeat "★" as many times as a number in state in React. 我想在React中重复“★”和状态数字一样多的次数。
What should I do? 我该怎么办?
I want to printing like this. 我想这样打印。
js ★★★ JS★★★
ts ★★ ts★★
html ★★★ HTML★★★
state = { myScores: [ { name: "js", score: 3 }, { name: "ts", score: 2 }, { name: "html", score: 3 } ] } ... const myScores = this.state.myScores.map( ({name, score}) => ( <div> {name} { for(let i=0; i<score; i++) { <div>★</div> } } </div> ) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
All you need to do is replace for loop with. 您需要做的就是替换循环。
<div>{"★".repeat(score)}</div>
Reason why your code was not working the way you expected is. 代码未按预期方式工作的原因是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.