Arrays 在React中每次添加元素时增加ID

Arrays 在React中每次添加元素时增加ID,arrays,reactjs,loops,Arrays,Reactjs,Loops,我遇到了一个问题,无法为行中每添加一个元素都增加state元素的ID。我得到的是,对于元素的每一个加法,重复相同的数字,我需要像ID这样的东西,应该是1到n个加法数字。 在文本框中,我没有输入ID,我只输入LText(名称)。 通常,每次添加元素时都应生成ID。 我试过的是 export default class EPIM091 extends cntrl.WITBase { constructor(props) { super(props);

我遇到了一个问题,无法为行中每添加一个元素都增加state元素的ID。我得到的是,对于元素的每一个加法,重复相同的数字,我需要像ID这样的东西,应该是1到n个加法数字。 在文本框中,我没有输入ID,我只输入LText(名称)。 通常,每次添加元素时都应生成ID。 我试过的是

    export default class EPIM091 extends cntrl.WITBase {
    constructor(props) {
        super(props);
        const witState = this.state;
        if (Object.keys(witState).length == 0) {
            witState.model = { LID: 1, LText: '', SOrder: '', Inventoried: false, Location:[] }; //Here i need to store all other state values to the Location Array
}
}
 clickAction = (e) => {
if (e.id == 'btn_add') {
            //var i = 1;
            const { model } = this.state;
            if (model.LText != null) {
                if ((model.Location || []).length == 0) {
                    model.Location = [];
                }
                // this.setState(prevState => { return { LID: e.id == 'btn_add' ? prevState.LID + 1 : prevState.LID - 1 } }); //This does not worked
                model.Location.push({ "LID": model.LID.toString(), "LText": model.LText, "SOrder": model.SOrder, "Inventoried": model.Inventoried.toString() });
                this.setState({
                    model: model,
                    LID: model.LID + 1 //This also not worked
                });
            }
        }
    };
       render(){
// Due to confusion of code, i did not add the textboxes codes
<cntrl.WITButton id="btn_add" onWitClick={this.clickAction} />
}
导出默认类EPIM091扩展cntrl.WITBase{
建造师(道具){
超级(道具);
const witState=this.state;
if(Object.keys(witState).length==0){
witState.model={LID:1,LText:'',SOrder:'',Inventoried:false,Location:[]};//这里我需要将所有其他状态值存储到位置数组中
}
}
单击操作=(e)=>{
如果(e.id==“btn\U添加”){
//var i=1;
const{model}=this.state;
如果(model.LText!=null){
if((model.Location | |[]).length==0){
model.Location=[];
}
//this.setState(prevState=>{return{LID:e.id=='btn_add'?prevState.LID+1:prevState.LID-1}});//这不起作用
model.Location.push({“LID”:model.LID.toString(),“LText”:model.LText,“SOrder”:model.SOrder,“Inventoried”:model.Inventoried.toString()});
这是我的国家({
模型:模型,
LID:model.LID+1//这也不起作用
});
}
}
};
render(){
//由于代码混乱,我没有添加文本框代码
}

我需要一些东西,比如当我添加时,我应该得到从1到添加元素数的唯一盖子。我得到的是相同的ID,即1。谢谢你这里有很多问题:

1) 您正试图修改声明为常量的变量:

const witState = this.state;
        if (Object.keys(witState).length == 0) {
            witState.model = { LID: 1, LText: '', SOrder: '', Inventoried: false, Location:[] };
if (model.LText != null) {
                if ((model.Location || []).length == 0) {
                    model.Location = [];
                }
                // this.setState(prevState => { return { LID: e.id == 'btn_add' ? prevState.LID + 1 : prevState.LID - 1 } }); //This does not worked
                model.Location.push({ "LID": model.LID.toString(), "LText": model.LText, "SOrder": model.SOrder, "Inventoried": model.Inventoried.toString() });
                this.setState({
                    model: model,
                    LID: model.LID + 1 //This also not worked
                });
            }
2) 您试图访问状态变量,但从未定义它:

const { model } = this.state;
3) 您尝试修改它,即使它声明为常量:

const witState = this.state;
        if (Object.keys(witState).length == 0) {
            witState.model = { LID: 1, LText: '', SOrder: '', Inventoried: false, Location:[] };
if (model.LText != null) {
                if ((model.Location || []).length == 0) {
                    model.Location = [];
                }
                // this.setState(prevState => { return { LID: e.id == 'btn_add' ? prevState.LID + 1 : prevState.LID - 1 } }); //This does not worked
                model.Location.push({ "LID": model.LID.toString(), "LText": model.LText, "SOrder": model.SOrder, "Inventoried": model.Inventoried.toString() });
                this.setState({
                    model: model,
                    LID: model.LID + 1 //This also not worked
                });
            }
除非有很多代码没有显示给我们,否则您的控制台中肯定有很多错误。请注意

-[Edit]-

问题是,除非我有完整的代码,否则我无法真正帮助您,因为这样做毫无意义。如果您只想增加LID state var,下面是一个工作示例:

constructor(props) {
   super(props);
   this.state={LID:1}
}
test=(e)=>{
    let {LID} = this.state;
    this.setState({LID:LID+1})
    console.log(this.state.LID);
 }
 render(){
     return(
         <button onClick={this.test}> Test</button>
     )
 }
构造函数(道具){
超级(道具);
this.state={LID:1}
}
测试=(e)=>{
设{LID}=this.state;
this.setState({LID:LID+1})
console.log(this.state.LID);
}
render(){
返回(
试验
)
}

您的代码格式不好,并且极难阅读您正在扩展的cntrl.WITBase是什么?并且您没有在构造函数中为组件提供任何状态。i)cntrl.WITBase是一个基类,无需担心。ii)const witState=this.state;我将此.state存储到witState并在构造函数中使用uctor。请参阅一次。在使用setState设置LID后,如何访问LID?我将所有值存储到位置[],并使用(数据源:model.Location)和(字段:“LText”),我访问元素并显示它。我只需要在添加元素后,LID应该由1生成到…添加行数。您了解我的问题吗?通过在setState中递增,我得到了解决方案,但react docs说不要在setState中递增。this.setState({LID:this.state.model.LID++},函数(){model.Location.push({“LID”:this.state.LID+1,“LText”:model.LText,“SOrder”:model.SOrder,“Inventoried”:model.Inventoried.toString()});this.setState({model:model})我用一个有效的例子编辑了我的答案,这样你就可以看到什么是有效的。