渲染方法在内部映射值时打破了 [英] Render Method Breaking When Mapping Value Inside

查看:87
本文介绍了渲染方法在内部映射值时打破了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件(一个下拉列表),它应该根据从另一个组件传入的数组作为classesprop填充列表。为了使其尽可能高效,我试图使用Object.keys和Array.prototype.map方法循环遍历我的数组,填充列表和渲染。但是,每当我添加这个组件,它会导致我的整个应用程序根本不渲染。我在下面列出了我的渲染方法。



渲染方法:

 导出默认值React.createClass({

更改:function(){
console.log(this.props.classes);
},

render:function(){

return(
< div>

< select onChange = {this.change}>
{
Object.keys .props.classes).map(value,key => {

return< option key = {key}> {value}< / option>
}
)}
< / select>
< / div>



}

});


解决方案

回调参数需要额外的括号我认为: p>

 导出默认值React.createClass({

更改:function(){
console.log (this.props.classes);
},

render:function(){

return(
< div>

< select onChange = {this.change}>
{
Object.keys(this.props.classes).map((value,key)=> {

return< option key = {key}> {value}< / option>
}
)}
< / select>
< / div>



}

});


I have a component (a dropdown list), which should populate the list based on an array which was passed in from another component as the "classes" prop. To make it as efficient as possible, I'm attempting to use Object.keys and Array.prototype.map methods to loop through my array, populate the list, and render. But, whenever I added this component, it causes my entire application to not render at all. I've listed my render method below.

Render Method:

export default React.createClass({

    change: function(){
        console.log(this.props.classes);
    },

    render: function(){

        return(
            <div>

             <select onChange = {this.change}>
             {
                Object.keys(this.props.classes).map(value, key =>{

                return <option key = {key}>{value}</option>
                }
             )}
              </select>
            </div>

        )

    }

});

解决方案

The callback parameters need extra parenthesis I think:

export default React.createClass({

    change: function(){
        console.log(this.props.classes);
    },

    render: function(){

        return(
            <div>

             <select onChange = {this.change}>
             {
                Object.keys(this.props.classes).map((value, key) =>{

                return <option key = {key}>{value}</option>
                }
             )}
              </select>
            </div>

        )

    }

});

这篇关于渲染方法在内部映射值时打破了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆