How to filter through array of components/elements in reactjs

前端 未结 2 754
栀梦
栀梦 2020-12-12 05:48

So I can get the button through the event when it is clicked on. But when I do a filter, it does not remove the said button.

So I have my array in the constructor():

相关标签:
2条回答
  • 2020-12-12 06:14

    The approach you are currently taking isn't really "react". You need to think more about a change in state rather than altering the dom directly.

    One approach would be:

    class App extends React.Component {
      constructor(){
        super();
        this.state ={
          visibleButtons: [ 11, 22, 33, 44 ],
          buttons: {
            11: {
              label: "Foo",
            },
            22: {
              label: "Bar"
            },
            33: {
              label: "Cow",
            },
            44: {
              label: "Pig"
            },        
          },
        }
      }
    
      onDelete(deletedId) {
        this.setState({
           visibleButtons: this.state.visibleButtons.filter(id => id !== deletedId)
        });
      }
    
      render () {                                        
        return (
          <div>
            { this.state.visibleButtons.map(buttonId => (
              <button key={buttonId} onClick={() => this.onDelete(buttonId)}>{this.state.buttons[buttonId].label}</button>
            )) }   
          </div>
        );
      }
    }
    
    ReactDOM.render(<App/>,document.getElementById('root'));
    

    http://codepen.io/cjke/pen/RKwWwZ?editors=0010


    Edit

    An example showing adding and removing. The unique id is pretty primitive, and doesn't actively check for what is there, but you should get the gist:

    class App extends React.Component {
      constructor(){
        super();
        this.onAdd = this.onAdd.bind(this);
        this.onChange = this.onChange.bind(this);
    
        this.state ={
          value: '',
          uniqueId: 100,
          visibleButtons: [ 11, 22, 33, 44 ],
          buttons: {
            11: {
              label: "Foo",
            },
            22: {
              label: "Bar"
            },
            33: {
              label: "Cow",
            },
            44: {
              label: "Pig"
            },        
          },
        }
      }
    
      onDelete(deletedId) {
        this.setState({
           visibleButtons: this.state.visibleButtons.filter(id => id !== deletedId)
        });
      }
    
      onChange(e) {
        this.setState({ value: e.target.value });
      }
    
      onAdd(e) {
        this.setState({
          uniqueId: this.state.uniqueId + 1,
          value: '',
          buttons: {
            ...this.state.buttons, 
            [this.state.uniqueId]: { 
              label: this.state.value,
            }
          },
          visibleButtons: [...this.state.visibleButtons, this.state.uniqueId],
        });
      }
    
      render () {                                        
        return (
          <div>
            <div>
            { this.state.visibleButtons.map(buttonId => (
              <button key={buttonId} onClick={() => this.onDelete(buttonId)}>{this.state.buttons[buttonId].label}</button>
            )) } 
            </div>
            <br/>
            <div>
              <input onChange={this.onChange} value={this.state.value}/><button onClick={this.onAdd}>+</button>
            </div>
          </div>
        );
      }
    }
    
    ReactDOM.render(<App/>,document.getElementById('root'));
    
    0 讨论(0)
  • 2020-12-12 06:21

    First of all, you need to bind this to the scope of your callback function. If you want to access the react object instance used to render the button from the synthetic event, you can do so using the private variable _targetInst.

    class Buttons extends React.Component{
    
      constructor(props) {
        super(props);
        this.delete_me = this.delete_me.bind(this);
            this.state = {
            buttons : [<button key="0" onClick={this.delete_me}>0</button>,<button key="1" onClick={this.delete_me}>1</button>]
        };
      }
    
      delete_me(e){
        const buttons = this.state.buttons.filter((button) => button != e._targetInst._currentElement);
        this.setState({ buttons });
      }   
    
      render() {
        return <div>{this.state.buttons}</div>;
      }
    };
    
    ReactDOM.render(
      <Buttons />,
      document.getElementById('container')
    );
    

    However, as Chris mentioned, your approach is not very much in line with the React patterns, and you should avoid accessing private methods or properties (usually named with an underscore)

    0 讨论(0)
提交回复
热议问题