Driftix

Cours react js 1

Nov 19th, 2021
800
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.   this.todoText.current.value = "";
  16. }
  17.   render(){
  18.           const todos = this.state.todos;
  19.     return (
  20.       <div>
  21.         <ul>{todos.map(todo => <li>{todo}</li>)}</ul>
  22.         <form onSubmit={this.addTodo}>
  23.          <input name="todo" placeHolder="aboule" type ="text" ref={this.todoText}/>
  24.          <input type="submit" value="Add Todo"/>
  25.           </form>
  26.           </div>
  27.       );
  28.   }
  29. }
  30.  
  31.  
  32. ReactDOM.render(<Saisie/>,document.getElementById('root'));
Advertisement
Add Comment
Please, Sign In to add comment