1

I have a series of data that contains some objects in one array(json file) and it will be shown in React. The main problem is that the value of index_ in onClick={e => this.handelruleRoom(e, element.families[j], index)} function in for loop always shows the last one. For example lenfamilies is 3 , when ever I click <span key={index} onClick={e => this.handelruleRoom(e, element.families[j], index)}>Click</span> with key=1in console the value of index_ is 3.

class App extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        data: [],
        DetailsInfo: {},
        text:{}
    }
}
render() {
    const { data } = this.state;
    const renderInfo = data.map((item, i) => {
        return (
            <div class="item">
                <div class="moreInfo" onClick={e => this.showDiv(e, item, i)}>
                    <span>show more data</span>
                </div>
                    <div class="table">{this.state.DetailsInfo[i]}</div>
                </div>
        )
    })
    return <div>{renderInfo}</div>;
}
showDiv = (e, element, i) => {
    this.setState(prevState => ({
        DetailsInfo: { ...prevState.DetailsInfo, [i]: this.renderDetails(element, i) },
    }))
}
renderDetails(element,i){
     let lenfamilies = element.families.length 
     var indents =[];
         var index=0;
     for(var j = 0 ;j <lenfamilies;j++){
         var numF = i;
         var numS = j;
         var stingF = numF.toString();
         var stingS = numS.toString();
         index= stingF+stingS
         indents.push(<div>
             <span  key={index} onClick={e => this.handelrule(e, element.families[j], index)}>Click</span>
             <span  class="text" key={index}>{this.state.text[index]}</span></div>
         )
     }
     return(
       indents
       )
     }
handelrule = (e, element, index) => {
console.log(index)
 this.setState( prevState => ({
     text: { ...prevState.text, [index]:   'test'},
     })) 
  }
}
 ReactDOM.render(<App />, document.getElementById("Result"));
1
  • Hi @Prakash Sharma.I uselet instead of var for j variable. but there is no different;| Commented Feb 24, 2019 at 8:14

1 Answer 1

1

You are creating a function inside a loop. You can avoid the for loop altogether by using forEach or map method like this

lenfamilies.forEach((fml, j) => {
         var numF = i;
         var numS = j;
         var stingF = numF.toString();
         var stingS = numS.toString();
         index= stingF+stingS
         indents.push(<span  key={index} onClick={e => this.handelrule(e, element.families[j], index)}>Click</span>
         )
     }
);

Or you can use let instead of var for j

for(let j = 0 ;j <lenfamilies;j++){
    ...
    indents.push(<span  key={index} onClick={e => this.handelrule(e, element.families[j], index)}>Click</span>)
}

UPDATE:-

For index, you can take index declaration inside the loop and use let for index too.

for(let j = 0 ;j <lenfamilies;j++){
    let index = 0;
    ....
Sign up to request clarification or add additional context in comments.

6 Comments

Using let instead of var for j did not solve the problem.
Thanks alot @Prakash Sharma. There is another issue. Can I ask you?
@bita If issue is completely different, it would be better to ask another dedicated question.
It is about this question.
Could you please take a look on my code. I want by clicking onClick={e => this.handelrule(e, element.families[j], index)} the text of <span class="text" key={index}>{this.state.text[index]}</span> be test and test is going to be dynamic. I put test just as testing, but does not set.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.