Mam ten komponent:
import React from 'react';
export default class AddItem extends React.Component {
add() {
this.props.onButtonClick(this.input.value);
this.input.value = '';
}
render() {
return (
<div className="add-item">
<input type="text" className="add-item__input" ref={(input) => this.input = input} placeholder={this.props.placeholder} />
<button disabled={!this.input.value} className="add-item__button" onClick={this.add.bind(this)}>Add</button>
</div>
);
}
}
Chcę, aby przycisk był wyłączony, gdy wartość wejściowa jest pusta. Ale powyższy kod nie działa. To mówi:
add-item.component.js: 78 Uncaught TypeError: Cannot read property „value” of undefined
wskazuje na disabled={!this.input.value}. Co tu robię źle? Zgaduję, że być może ref nie jest jeszcze tworzony, gdy rendermetoda jest wykonywana. Jeśli tak, jakie jest obejście?
