0

So I have a component, called AddButton

export default class AddButton extends React.Component {
  constructor(props) {
    super(props);
  }

  addItem(e) {
    this.btn.setAttribute('disabled', 'disabled');
    this.props.addItem(e.target.getAttribute('data-row-index'))
  }

  render() {
    return (
      <div className="row">
        <div className="col-md-12 text-center">
          <button ref={btn => {this.btn = btn }} className="btn btn-success" onClick={this.addItem.bind(this)} data-row-index={this.props.rowIndex}>Add</button>
        </div>
      </div>
    )
  }
}

Some where else in the code I do:

if (this.props.addButton) {
  rows.push(
    <td key="add">
      <AddButton
        addItem={this.props.addItem}
        rowIndex={this.props.rowIndex}
      />
    </td>
  )
}

So I have at one time 50 of these in a table at the end of the row. When one is clicked I wanted to disable all the buttons.

So as you can see I have done, in addItem(e):

addItem(e) {
  this.btn.setAttribute('disabled', 'disabled');
  this.props.addItem(e.target.getAttribute('data-row-index'))
}

But when I test this, only the button after the one is clicked is disabled. I want them all to be disabled

Any way I could modify this to achieve that?

1 Answer 1

1

React components have syntax like HTML DOM but they are not, they are modules and every time you use that module for example in your table it would be a new instance of that module. So if you want to share a state between them you have to pass it as props to them and you shouldn't treat them as HTML nodes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.