I have button which by default was disabled. But when the checkbox was selected, button will be enabled, using ref i am enabling the button. After the button activated click event is not firing. Without the default disable, button's click event was firing.
<button
className="btn btn-danger"
ref={element => {
this.actionButtonRef = element;
}}
onClick={event => {
console.log("clicked");
}}
disabled=""><Trans>DELETE_ALL</Trans></button>
<th key="checkboxall">
<input
type="checkbox"
ref={element => {
this.checkboxAllRef = element;
}}
onChange={event => this.onChangeCheckBox(event)}/>
</th>
onChangeCheckBox(event) {
if(event.target.checked) {
this.actionButtonRef.disabled = "";
}
else {
this.actionButtonRef.disabled = "disabled";
} }
Here is the jsfiddle jsfiddle.net/Sivabalan/en1fax58
If anything i did wrong means, let me help guys
React will interpret the empty string as you adding the attribute. Thus, you need to explicitly tell it to be false disabled={false}. React will then remove the attribute completely. See here: github.com/facebook/react/issues/9230
I think you tried to swim against the stream. You tried to manipulate things directly which isn't optimal and can lead to side effects.
I use React with some rules in mind because the way React works.
The order is:
and the cycle restarts.
I've altered your code on how I would code your component. Have a look.
Any Questions or feedback is appreciated
Don't directly manipulate any of the DOM that is rendered by React. When you change the
disabledattribute, React is likely to overwrite your change. The good news is that direct DOM manipulation is totally unnecessary.Use
propsandstateto control the DOM.In the following code, the
disabledattribute of the button is controlled by thedisabledproperty of the component'sstate. When the checkbox is toggled, theonChangeDisabledmethod is called, which sets thedisabledproperty of the component'sstate(viasetState) to the opposite of the checkbox'scheckedstate. Importantly, the call tosetStatetriggers a re-render of the component, which applies the latest value ofthis.state.disabledto everything that refers to it inrender.class MyComponent extends React.Component { constructor(props) { super(props); this.onChangeDisabled = this.onChangeDisabled.bind(this); this.onDeleteAll = this.onDeleteAll.bind(this); this.state = { disabled: props.initialDisabled || false }; } onChangeDisabled(e) { this.setState({disabled: !e.target.checked}); } onDeleteAll(e) { this.props.onDeleteAll(); } render() { return ( <div> <button disabled={this.state.disabled} onClick={this.onDeleteAll} > <Trans>DELETE_ALL</Trans> </button> <input type="checkbox" checked={!this.state.disabled} onChange={this.onChangeDisabled} /> </div> ); } } MyComponent.propTypes = { initialDisabled: PropTypes.bool, onDeleteAll: PropTypes.func }; MyComponent.defaultProps = { onDeleteAll: () => {} };Or if you're using the latest ES features:
class MyComponent extends React.Component { static propTypes = { initialDisabled: PropTypes.bool, onDeleteAll: PropTypes.func }; static defaultProps = { onDeleteAll: () => {} }; state = { disabled: this.props.initialDisabled || false }; onChangeDisabled = ({target: {checked}}) => { this.setState({disabled: !checked}); }; onDeleteAll = () => { this.props.onDeleteAll(); }; render() { return ( <div> <button disabled={this.state.disabled} onClick={this.props.onDeleteAll} > <Trans>DELETE_ALL</Trans> </button> <input type="checkbox" checked={!this.state.disabled} onChange={this.onChangeDisabled} /> </div> ); } }Alternatively, you can control
disabledfrom the outside so thatMyComponentbecomes stateless:const MyComponent = ({ disabled = false, onChangeDisabled = () => {}, onDeleteAll = () => {} }) => ( <div> <button disabled={disabled} onClick={() => void onDeleteAll()} > <Trans>DELETE_ALL</Trans> </button> <input type="checkbox" checked={!disabled} onChange={({target}) => void onChangeDisabled(!target.checked)} /> </div> ); MyComponent.propTypes = { initialDisabled: PropTypes.bool, onDeleteAll: PropTypes.func };