如何在React中渲染组件的数组(字符串)?

时间:2018-05-11 18:49:03

标签: javascript reactjs

我在加载页面时尝试渲染文本数组:

'values': ['hello', 'world']

但它给了我一个错误。在我的formatoc.js中,我有:

var props = {
    'name' : 'form',
    'timer' : 1500,
    'callback' : function(id, validity, value) {console.log(id, validity, value);},
    'values': ['hello', 'world'],
    'newParent' : new FormatOC.Parent({

        "one": {"__array__":"unique", "__type__":"string","__minimum__":1,"__maximum__":200,"__component__":"Input"}

    })

}

React.render(React.createElement(ParentComponent, props), document.getElementById('react-component'));

这就是我在Parent.jsx文件中的内容:

define([' react',' r_foc_node'],function(React,NodeComponent){

var ParentComponent = React.createClass({

    getInitialState: function() {

        if(!this.props.newParent) {
            throw "ParentComponent requires newParent property";
        }

        return {
            "values": {}
        }
    },

    recursive : function(level) {

        that = this;

        console.log(level);

        for (keys in level) {
            console.log("This is a key: ", keys);

            if ((typeof level[keys]) === "object") {
                if (level[keys].constructor.name === "Parent") {
                    console.log('I am a parent');
                    level = level[keys].details();
                    //console.log(level[keys].get([key_list[0]]));
                    this.recursive(level);
                    //console.log(level[keys].keys());
                } else {
                    console.log('I am a node');
                    //console.log(level[keys]);
                };

            };

        }
    },

    childChange: function(name, valid, value) {

        this.state.values[name] = value;
        this.setState(this.state);

        console.log(name,value);


        // Call parent callback
        this.props.callback(
            this.props.name,
            this.props.newParent.valid(this.state.values),
            this.state.values
        );
    },

    render: function() {
        var that = this;

        var level = this.props;

        return (
            <div id = "form">
            {level.newParent.keys().map(function(k) {
                return (
                    <div>
                    {(level.newParent.get(k).constructor.name === "Parent") ?
                    <ParentComponent
                        name={k}
                        key={k}
                        timer={that.props.timer}
                        callback={that.childChange}
                        values={that.props.values[k]}
                        newParent={that.props.newParent.get(k)}
                    />
                    :
                    <NodeComponent
                        name={k}
                        key={that.props.name + '.' + k}
                        timer={that.props.timer}
                        callback={that.childChange}
                        values={that.props.values[k]}
                        newNode={that.props.newParent.get(k)}
                    />
                    }
                    </div>
                )
            })
            }
            </div>
        )
    }

我不确定错误是否是因为我尝试访问数组值的方式?但我似乎无法呈现helloworld这些字词。

1 个答案:

答案 0 :(得分:2)

您正在使用:level.newParent.keys().map(function(k) {...
这应该是:level.values.map(function(k) {...