简体   繁体   English

如何在 ReactJS 中添加按钮组件?

[英]How do I add a button component in ReactJS?

So I'm rendering components from an array 'values': ["hello", "world] successfully but I would like to add a button component so that every time it gets clicked, another empty field shows up. This is what it currently looks like:因此,我正在从数组'values': ["hello", "world]成功渲染组件,但我想添加一个按钮组件,以便每次单击它时,都会显示另一个空字段。这就是目前的情况好像:

截屏

but i would like it so that there is a button and every time I click on it, it renders another empty component to input text.但我希望它有一个按钮,每次我点击它时,它都会呈现另一个空组件来输入文本。 Would it be correct to add a button component directly inside the my array_node.jsx file?直接在我的 array_node.jsx 文件中添加按钮组件是否正确? Is what I'm doing correct so far?到目前为止我所做的是否正确? Would I also have to add some sort of newInput: function() in side the var AddButton = React.createClass({}) ?我是否还必须在var AddButton = React.createClass({})旁边添加某种newInput: function() var AddButton = React.createClass({}) Thank you!谢谢!

array_node.jsx: array_node.jsx:

{...
    childChange: function(name, valid, value) {

        // update state
        this.state.values = this.props.values;

        // Using regex to find last digits from 0-9
        var pattern = /[0-9]/;
        var match = name.match(pattern);

        // Parse char into int
        var i = parseInt(match);

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

        // Call parent callback
        this.props.callback(
            this.props.name,
            this.props.node.valid(this.state.values),
            this.state.values
        );
    },
    addItem: function(values){
    },

        render: function() {
            var that = this;

            return (
                <div id = "form">
                {this.props.values.map(function(v, i) {
                    return (
                        <div>
                        {(that.props.node.get().constructor.name === "Parent") ?
                        <ParentComponent
                            name={that.props.name + i}
                            key={i}
                            timer={that.props.timer}
                            callback={that.childChange}
                            values={v}
                            newParent={that.props.node.get()}
                        />
                        :
                        <NodeComponent
                            name={that.props.name + i}
                            key={i}
                            timer={that.props.timer}
                            callback={that.childChange}
                            value={v}
                            newNode={that.props.node.get()}
                        />
                        }
                        </div>
                    )
                })}
                </div>
           )
        }
    });
    return ArrayNodeComponent


var AddButton = React.createClass({
    addItem: function() {

    },

    render: function() {

        return(
        <div id="create_new_entry">

        </div>
        )
    }
})

formatoc:格式:

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

    'node' : new FormatOC.ArrayNode({"__array__":"unique", "__type__":"string","__minimum__":1,"__maximum__":200,"__component__":"Input"},
    )
}

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

You might add a button into your form within the render function.您可以在渲染函数中向表单中添加一个按钮。 Then listen to clicks and add a new empty element to your values list.然后听点击并将一个新的空元素添加到您的值列表中。

if you would like to propagate the changes to some parent component, you would have to pass the onClick handler from the parent component and update the values list there too.如果您想将更改传播到某个父组件,则必须从父组件传递onClick处理程序并更新那里的值列表。

 import { Component } from 'react'; class ArrayNodeComponent extends Component { // other code ... // like your initialisation of your state // and other functions addEmptyItem() { const { values } = this.state; this.setState({ values: [...values, ""] }); } render() { return ( <form id="test"> { /* this is your values map routine, shortened */ this.props.values.map(function(v, i) { /*...*/ }) } <button onClick={() => this.addEmptyItem()}>Add</button> </form> ); } }

Btw in this simple scenario, it would not make sense to create a custom Button component.顺便说一句,在这个简单的场景中,创建自定义 Button 组件是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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