简体   繁体   中英

How do I render an array (strings) of components in React?

I'm trying to render the array of text when I load the page:

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

but it's giving me an error. In my formatoc.js , I have:

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'));

This is what I have in my Parent.jsx file:

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>
        )
    }

I'm not sure if the error is because of the way I'm trying to access the array values? But I can't seem to get the words hello and world to be rendered.

You are using: level.newParent.keys().map(function(k) {...
this should be: level.values.map(function(k) {...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM