How to assign refs to multiple components

前端 未结 4 1335
萌比男神i
萌比男神i 2020-11-27 20:26

I\'m using React to render multiple data using array.map.
How can disable the clicked button from the list?

This is my code:

  onRun         


        
4条回答
  •  清酒与你
    2020-11-27 20:42

    You can use the npm module react-multi-ref (a tiny library by me) to do this.

    import React from 'react';
    import MultiRef from 'react-multi-ref';
    
    class Foo extends React.Component {
      _actRefs = new MultiRef();
    
      onRunClick(act, e) {
        this._actRefs.map.get(act._id).setAttribute("disabled", true);
      }
    
      render () {
        return (
          
    { this.state.acts.map((act) => { let boundActRunClick = this.onRunClick.bind(this, act); return (

    Name: {act.name}, URL(s): {act.urls}

    ) }) }
    ); } }

    Though in this specific case where you just want to change an attribute on an element, instead of using a ref you should do it through state and props on the

提交回复
热议问题