reactjs - React calling a function inside the render method leads to endless call stack -


import react, {component} 'react' import ajax '../confing/ajax' import $ 'jquery'  class categories extends component {     constructor(props) {         super(props);         this.state = {             isactive: '',             categories: ''         };         this.switchlinktoactive = this.switchlinktoactive.bind(this);     }      switchlinktoactive(event) {         event.preventdefault();         let temp = [];         console.log(event.target.id);         (let = 0; < this.state.isactive.length; i++) {             if (i === parseint(event.target.id)) {                 temp[i] = true;             } else {                 temp[i] = false;             }         }         this.setstate({             isactive: temp         })      }      func() {         let categories = [];         ajax.request.get('categories').then((resp) => {             let isactive = new array(resp.length);             (let = 0; < resp.length; i++) {                 isactive[i] = false;             }             this.setstate({                 isactive: isactive             });             (let = 0; < resp.length; i++) {                 let element = resp[i];                 categories.push(<a key={element.id} href=""                                    classname={this.state.isactive[i] === true ? "list-group-item list-group-item-action active" : "list-group-item list-group-item-action"}                                    id={i} onclick={this.switchlinktoactive}>{element.name}</a>);              }             this.setstate({                 categories: categories             });         });     }      render() {        this.func();         return (             <div classname="list-group col-3">                 {/*{this.state.categories}*/}                 <a key={2} href=""                    classname={this.state.isactive[2] === true ? "list-group-item list-group-item-action active" : "list-group-item list-group-item-action"}                    id={2} onclick={this.switchlinktoactive}>custom lemenet</a>             </div>         )     } }  export default categories; 

in example calling func() inside of render() method , leads similar stackoverflow, doesn't stop calling it. anyway fix that? didn't find solutions that. if lets loop inside of render works intended it's called once, why function called endless amount of times

you have make ajax calls in componentdidmount. below

componentdidmount() {  this.func(); } 

in ajax calls, once response, set state. func below

func() {     let categories = [];     ajax.request.get('categories').then((resp) => {         (let = 0; < resp.length; i++) {             resp[i].isactive = false;         }         this.setstate({             data : resp,         });     }); } 

your render use data in state , render the required dom structure appropriately.

render() {      return (         <div classname="list-group col-3">              {              (this.state.data || []).map((element, idx) =>                categories.push(<a key={element.id} href=""                                classname={element.isactive ? "list-group-item list-group-item-action active" : "list-group-item list-group-item-action"}                                id={idx} onclick={this.switchlinktoactive}>{element.name}</a>);               })            }              <a key={2} href=""                classname={this.state.isactive[2] === true ? "list-group-item list-group-item-action active" : "list-group-item list-group-item-action"}                id={2} onclick={this.switchlinktoactive}>custom lemenet</a>         </div>     ) } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -