Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置状态以生成数组结果_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 设置状态以生成数组结果

Javascript 设置状态以生成数组结果,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,如何在onChange事件中将索引推入数组位置?假设用户可以输入任何一个输入,我可以得到数组中的值,位置必须与输入的索引匹配 onChange = () => { //so that I get, console.log(this.state.inputGroup) //expected ['value1', 'value2', 'value3'] } render() { return( <div> <i

如何在onChange事件中将索引推入数组位置?假设用户可以输入任何一个输入,我可以得到数组中的值,位置必须与输入的索引匹配

onChange = () => {
    //so that I get, console.log(this.state.inputGroup)
    //expected ['value1', 'value2', 'value3']
}

render() {
    return(
        <div>
            <input onChange={e => this.onChange(1, index)} type="text"/>
            <input onChange={e => this.onChange(2, index)} type="text"/>
            <input onChange={e => this.onChange(3, index)} type="text"/>
        </div>
    )
}
onChange=()=>{
//因此,我得到console.log(this.state.inputGroup)
//预期值['value1'、'value2'、'value3']
}
render(){
返回(
this.onChange(1,index)}type=“text”/>
this.onChange(2,索引)}type=“text”/>
this.onChange(3,索引)}type=“text”/>
)
}

您需要将值从输入传递到
onChange
方法。 所以你要传递
e.target.value
。固定索引是第一个参数,这就是指向数组索引的方式

{this.onChange(1,e.target.value)}type=“text”/>

onChange
方法中

onChange = (inputIndex, textValue) => {
   //I assume that array of size equal to inputs quantity already is 
   //declared with ex. empty strings (you can initialize that in 
   //component constructor)

   const inputGroup = this.state.inputGroup
   inputGroup[inputIndex] = textValue

   this.setState({ inputGroup })
}

顺便说一句。您可能希望以0:)开始输入索引化,否则数组中的0元素将永远不会被任何输入使用。

您需要将值从输入传递到
onChange
方法。 所以你要传递
e.target.value
。固定索引是第一个参数,这就是指向数组索引的方式

{this.onChange(1,e.target.value)}type=“text”/>

onChange
方法中

onChange = (inputIndex, textValue) => {
   //I assume that array of size equal to inputs quantity already is 
   //declared with ex. empty strings (you can initialize that in 
   //component constructor)

   const inputGroup = this.state.inputGroup
   inputGroup[inputIndex] = textValue

   this.setState({ inputGroup })
}
顺便说一句,您可能希望以0:)开始输入索引化,否则数组中的0元素将永远不会被任何输入使用。

类测试扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
inputGroup:[null,null,null]
}
}
onChange(索引,e){
const inputGroupState=this.state.inputGroup;
inputGroupState[索引]=e.target.value;
这是我的国家({
inputGroup:inputGroupState
},
() => {
console.log(this.state.inputGroup)
}
);
}
render(){
报税表(
this.onChange(0,e)}type=“text”/>
this.onChange(1,e)}type=“text”/>
this.onChange(2,e)}type=“text”/>
)
}
}
ReactDOM.render(,document.querySelector(“app”))
正文{
背景:#20262E;
填充:20px;
字体系列:Helvetica;
}
#应用程序{
背景:#fff;
边界半径:4px;
填充:20px;
过渡:均为0.2s;
}

类测试扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
inputGroup:[null,null,null]
}
}
onChange(索引,e){
const inputGroupState=this.state.inputGroup;
inputGroupState[索引]=e.target.value;
这是我的国家({
inputGroup:inputGroupState
},
() => {
console.log(this.state.inputGroup)
}
);
}
render(){
报税表(
this.onChange(0,e)}type=“text”/>
this.onChange(1,e)}type=“text”/>
this.onChange(2,e)}type=“text”/>
)
}
}
ReactDOM.render(,document.querySelector(“app”))
正文{
背景:#20262E;
填充:20px;
字体系列:Helvetica;
}
#应用程序{
背景:#fff;
边界半径:4px;
填充:20px;
过渡:均为0.2s;
}


这太零碎了,我们无法提供有用的帮助。(例如,
this.state.inputGroup
是什么?为什么需要输入索引?等等。不要只回答这些问题,请提供完整的解释。)请使用演示问题的工具更新您的问题,最好是使用堆栈片段运行的工具(
[]
工具栏按钮)。堆栈代码段支持React,包括JSX。这太零碎了,我们无法提供有效的帮助。(例如,
this.state.inputGroup
是什么?为什么需要输入索引?等等。不要只回答这些问题,请提供完整的解释。)请使用演示问题的工具更新您的问题,最好是使用堆栈片段运行的工具(
[]
工具栏按钮)。堆栈代码段支持React,包括JSX。为什么要投反对票?:)我相信这正是OP想要的。让他在表决前发言:|第一个索引可以是第一个或第二个参数,第二个inputGroup是常量,您可以如何修改它?
const
array就可以了。它不能作为一个整体对象被覆盖,但它的部分可以很好地被写入/读取<代码>常量不要使对象不可变。不过,我没有得到你评论的第一部分。第一个索引如何可以是第一个或第二个?我定义了方法,因此它是第一个参数,并将其作为第一个参数传递。怎么会是第二个呢?好了,我同意了!为什么要投反对票?:)我相信这正是OP想要的。让他在表决前发言:|第一个索引可以是第一个或第二个参数,第二个inputGroup是常量,您可以如何修改它?
const
array就可以了。它不能作为一个整体对象被覆盖,但它的部分可以很好地被写入/读取<代码>常量不要使对象不可变。不过,我没有得到你评论的第一部分。第一个索引如何可以是第一个或第二个?我定义了方法,因此它是第一个参数,并将其作为第一个参数传递。怎么会是第二个呢?好了,我同意了!