Search
 
SCRIPT & CODE EXAMPLE
 

HTML

how to delete a html tag element in react

const Row = function(props){
  const {checked, value, onChange, onChecked} = props;
  return (
    <div>
      <input 
        type="checkbox" 
        checked={checked}
        onChange={onChecked}
        />
      <input type ="text" value={value}  onChange={onChange}/>
    </div>
  );
}

class App extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      rows: [
        {value: 'row1', checked: false},
        {value: 'row2', checked: true},
        {value: 'row3', checked: false},
      ]
    };
  }
  
  updateValue = (e, idx) => {
    const rows = [...this.state.rows];  // copy array because we don't want to mutate the previous one
    rows[idx].value = e.target.value;
    this.setState({
        rows,
    });
  }
  
  onChecked = (idx) => {
    const rows = [...this.state.rows];  // copy array because we don't want to mutate the previous one
    rows[idx].checked = !rows[idx].checked;
    this.setState({
        rows,
    });
  } 
  
  addRow = () => {
    const rows = [...this.state.rows, 
                  {value:'', checked: false}
                 ];
    this.setState({
        rows,
    });
  }
  
  deleteRows = () => {
    this.setState({
      rows: this.state.rows.filter(e => !e.checked)
    });
  }
 
  render(){
    return(
      <div>
        {this.state.rows.map((row, idx) => {
          return(
              <Row 
                key={idx} 
                value={row.value}
                checked={row.checked}
                onChange={(e) => this.updateValue(e, idx)} 
                onChecked={() => this.onChecked(idx)}
                /> 
            )
        })
        }
        <button onClick={this.addRow}>
          add 
        </button>
        <button onClick={this.deleteRows}>
          delete
        </button>
      </div>
    );
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
);
Comment

PREVIOUS NEXT
Code Example
Html :: html text popup 
Html :: how to stop audio when playing other in html 
Html :: space in html 
Html :: static html template 
Html :: year picker in html 
Html :: html mapping coordinates placing 
Html :: <code html tag 
Html :: strong and bold difference in html 
Html :: how to start in html 
Html :: input radio button html 
Html :: change text color in bootstrap 
Html :: html to flutter 
Html :: ubuntu 17.04 vmware 
Html :: Przezroczysty div 
Html :: ## File * [random-numbers-unsolved](Unsolved/random-numbers-unsolved.html) ### Instructions * Research how to improve on Math.random() to generate a random whole number between 1 and 10 instead of a random decimal number 
Html :: html ssh terminal 
Html :: how to create a currency select options on a html website 
Html :: eleventy collection get url 
Html :: which of the following html tags are valid child items for the table elements? 
Html :: span element in html 
Html :: html to text 
Html :: convert haml to html 
Html :: brackeys C# 
Html :: Html countup from a date 
Html :: how to inser code in html 
Html :: input type number is not showing decimal cases when is 0.00 html 
Html :: gold color hash code 
Html :: should the logo be inside the ul in htm 
Html :: how to add send call in html 
Html :: html get button text 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =