Ho una coppia di componenti padre e figlio che mi ha bloccato con l'associazione dati.React e associazione dati
Un componente di livello superiore renderà un nuovo componente Question
:
<Question updateScore={this.updateScore} />
Procedimento updateScore
vuole aggiornare un hash con valori numerici. Non è troppo importante. La componente domanda è abbastanza semplice:
var Question = React.createClass({
getInitialState: function() {
return { options: blahBlahBlah };
},
updateScore: function(optionLabel) {
this.props.updateScore(optionLabel);
},
render: function() {
var optionList = this.state.options.map(function(option) {
return (
<QuestionItem optionLabel={option.optionLabel} description={option.description}
updateScore={this.updateScore} key={option.key} />
);
}.bind(this));
return (
<ul>
{optionList}
</ul>
);
}
});
La voce componenet domanda è ancora più semplice:
var QuestionItem = React.createClass({
render: function() {
return (
<li onClick={this.props.updateScore.bind(this, this.props.optionLabel)}>
{this.props.description}
</li>
);
}
});
Il problema è con l'implementazione corrente, la console sputa fuori questo errore:
"Warning: bind(): React component methods may only be bound to the component instance. See Question"
Anche se registro il valore della partitura che viene aggiornata, vedo che non aggiorna nessuno dei tasti ma inserisce un indefinito:
{ labelOne: 0, labelTwo: 0, undefined: NaN }
Come dovrei fare per legare questo?
Whoops che era un errore di battitura. Non ha quei paren. – Simpleton
Ah capisco, dovrei cancellare la mia risposta, allora? @Simpleton –
No, penso ancora che le tue parole possano essere utili agli altri. – Simpleton