How to filter through array of components/elements in reactjs

前端 未结 2 764
栀梦
栀梦 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 (
          
    { this.state.visibleButtons.map(buttonId => ( )) }
    ); } } ReactDOM.render(,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 (
          
    { this.state.visibleButtons.map(buttonId => ( )) }

    ); } } ReactDOM.render(,document.getElementById('root'));

提交回复
热议问题