Javascript 如何在ReactJS中添加按钮组件?
因此,我成功地呈现了数组Javascript 如何在ReactJS中添加按钮组件?,javascript,reactjs,Javascript,Reactjs,因此,我成功地呈现了数组'values':[“hello”,“world]中的组件,但我想添加一个按钮组件,以便每次单击它时,都会显示另一个空字段。这是它当前的样子: 但我希望有一个按钮,每次我点击它时,它都会呈现另一个空组件来输入文本。直接在my array_node.jsx文件中添加一个按钮组件是否正确?到目前为止我所做的是否正确?我是否还必须添加某种newInput:function()在var AddButton=React.createClass({})侧边,谢谢 array\u n
'values':[“hello”,“world]
中的组件,但我想添加一个按钮组件,以便每次单击它时,都会显示另一个空字段。这是它当前的样子:
但我希望有一个按钮,每次我点击它时,它都会呈现另一个空组件来输入文本。直接在my array_node.jsx文件中添加一个按钮组件是否正确?到目前为止我所做的是否正确?我是否还必须添加某种newInput:function()
在var AddButton=React.createClass({})
侧边,谢谢
array\u 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>
)
}
})
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'));
您可以在render函数中向窗体中添加一个按钮。 然后收听单击并将一个新的空元素添加到值列表中 如果要将更改传播到某个父组件,则必须从父组件传递
onClick
处理程序,并更新其中的值列表
从'react'导入{Component};
类ArrayNodeComponent扩展了组件{
//其他代码。。。
//就像你对自己国家的初始化一样
//和其他功能
addEmptyItem(){
const{values}=this.state;
这是我的国家({
值:[…值,“]
});
}
render(){
返回(
{
/*这是您的值映射例程,简称*/
this.props.values.map(函数(v,i){/*…*/})
}
this.addEmptyItem()}>Add
);
}
}
您的代码似乎非常过时。您使用React.createClass
而不是es6类有什么原因吗?如果您开始学习React,我强烈建议您使用es6语法。除了一些奇怪的项目需求外,我已经很久没有人见过像这样编写React代码了。绝对是什么@trixn说,你可能需要阅读一些更现代的例子,看看react文档,它们非常适合入门