猿问

React中ReactDOM.render参数语法错误

新学JSX,React 不太懂。。求指教

let names = ['Python', 'Java', 'C++', 'Javascript', 'C'];      

        ReactDOM.render(

            <div>

                {   //为什么这里报错 " }expected" 呢

                    

                    for(let name of names){

                        +function (){

                             return <h1>{name}</h1>;

                          }();

                    }

                   

                }

            </div>,

            document.getElementById('example')

        );


哔哔one
浏览 514回答 1
1回答

慕仙森

{ }&nbsp;是 jsx 中的模版插值语法。里面不管是写变量、表达式还是函数,都要确保有返回值。for有返回值吗?一般是用&nbsp;map&nbsp;遍历数据:<div>&nbsp; &nbsp; {arr.map(item => {&nbsp; &nbsp; &nbsp; &nbsp; return <h1>{item}</h1>&nbsp; &nbsp; })}</div>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答