Driftix

Cours react js 2

Nov 19th, 2021
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Saisie extends React.Component{
  2.  
  3.    constructor(props) {
  4.     super(props);
  5.     this.state = {
  6.       'todos':[]
  7.     };
  8.      this.addTodo = this.addTodo.bind(this);
  9.      this.todoText = React.createRef();
  10.    }
  11.  
  12. addTodo(event){
  13.   event.preventDefault();
  14.   //this.setState({'todos': [...this.state.todos, this.todoText.current.value]});
  15.   // Ou
  16.   const newItems = this.todoText.current.value;
  17.   this.state.todos.push(newItems);
  18.   this.setState({'todos' : this.state.todos});
  19.   this.todoText.current.value = "";
  20. }
  21.   render(){
  22.           const todos = this.state.todos;
  23.     return (
  24.       <div>
  25.         <ul>{todos.map(todo => <li>{todo}</li>)}</ul>
  26.         <form onSubmit={this.addTodo}>
  27.          <input name="todo" placeHolder="Saisir une action" type ="text" ref={this.todoText}/>
  28.          <input type="submit" value="Ajouter à la liste"/>
  29.           </form>
  30.           </div>
  31.       );
  32.   }
  33. }
  34.  
  35. ReactDOM.render(<Saisie/>, document.getElementById('root'));
Advertisement
Add Comment
Please, Sign In to add comment