2014-11-23 31 views
5

Sto provando a passare oggetti di scena da un componente figlio a un componente padre, ma per qualche motivo sto ricevendo l'errore "TypeError: this.props.onClick non è una funzione "nella console js dopo aver fatto clic sui componenti figlio resi.React.js - Funzione superata "non a funzione" Errore

In sostanza, ciò che sto cercando di fare è gestire un elenco di categorie in cui un sottocomponente di categoria ha il puntatore "selezionato" come vero e si aggiorna correttamente ogni volta che si fa clic su una categoria li.

Ecco un jsfiddle con il codice: http://jsfiddle.net/kb3gN/8023/

Non mi sento come se avessi effettivamente ancora afferrato i concetti di come utilizzare React.js, quindi ogni aiuto è apprezzato!

Cordiali saluti e grazie in anticipo,

bnunamak

Codice:

var CategoryList = React.createClass({ 

getInitialState:function(){ 
    return { 
     items:[ 
      ["cat1", false], 
      ["cat2", true] 
     ], 
     lastToggled:["cat2"] 
    } 
}, 

//Function passed to child (changes items state) 
handleClick:function(child){ 
    var tempItems = this.state.items; 
    if(this.state.lastToggled[0] === child.props.name){ 
     var index = this.getInd(this.state.tempItems, child.props.name); 
     tempItems[index][1] = !tempItems[index][1]; 

     this.setState({items: tempItems}); 
    }else{ 
     var newIndex = this.getInd(this.state.tempItems, child.props.name); 
     tempItems[newIndex][1] = !tempItems[newIndex][1]; 

     var oldIndex = this.getInd(this.state.tempItems, this.state.lastToggled[0]); 
     tempItems[oldIndex][1] = false; 

     this.setState({items: tempItems, lastToggled: tempItems[newIndex][0]}); 

    } 
}, 

//getInd not relevant to problem 
getInd:function(arr, elname){ 
    for (var i = arr.length - 1; i >= 0; i--) { 
     if(arr[i][0] === elname){ 
      return i; 
     } 
    }; 
    return -1; 
}, 

render:function(){ 
    return (<ul className="category-list"> 
     { 
      this.state.items.map(function(item){ 
       return <Category onClick={this.handleClick} name={item[0]} selected={item[1]} />; 
      }) 
     } 
     </ul>) 
} 

}); 


var Category = React.createClass({ 

render:function(){ 
    if(this.props.selected){ 
     return (<li onClick={this.propogateClick} className="selected">{this.props.name}</li>); 
    }else{ 
     return (<li onClick={this.propogateClick}>{this.props.name}</li>); 
    } 
}, 

propogateClick: function(){ 
    this.props.onClick(this); 
} 

}); 


React.renderComponent(<CategoryList/>, document.getElementById('example')); 

risposta

0

A quanto pare si tratta di un problema di portata qui:

this.state.items.map(function(item){ 
      return <Category onClick={this.handleClick} name={item[0]} selected={item[1]} />; 
     }) 

Se rimuovo la mappa esterna e aggiungere una categoria generica funziona (beh, ci sono altri problemi, ma la funzione click è s passato con successo).

Dovrò passare la funzione in un altro modo (non utilizzando questo.handleClick).

2

È possibile utilizzare legano per risolvere questo problema

this.state.items.map(function(item){ 
    return <Category onClick={this.handleClick} name={item[0]} selected={item[1]} />; 
}.bind(this)) 
Problemi correlati