Created
August 4, 2017 11:52
-
-
Save elgamine-dev/c5933314b5da9a2d7e8962ac58f43e4c to your computer and use it in GitHub Desktop.
React - passage de props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
class App extends React.Component { | |
constructor(){ | |
super(); | |
this.state = { | |
tasks : [ | |
{id:1, title: "Faire la lessive"}, | |
{id:2, title: "Manger", fait:true}, | |
{id:3, title: "Faire prout"}, | |
] | |
} | |
} | |
removeItem(id){ | |
let found = this.state.tasks.find((elt)=>{ | |
return elt.id === id; | |
}) | |
let pos = this.state.tasks.indexOf(found) | |
this.state.tasks.splice(pos, 1); | |
this.setState({tasks: this.state.tasks}); | |
} | |
render(){ | |
return ( | |
<div> | |
<Todo data={this.state.tasks} removeItem={this.removeItem.bind(this)} /> | |
</div> | |
) | |
} | |
} | |
class Todo extends React.Component { | |
deleteTask(id){ | |
// on fait notre suppression sur le serveur | |
// $.post('/api/tasks/delete', { id : id}, ()=>{ | |
// console.log("elt supprimé sur le serveur") | |
// }) | |
// | |
this.props.removeItem(id) | |
} | |
render(){ | |
let tasks = this.props.data.map((task)=>{ | |
return <li>{task.title} <button onClick={()=>{this.deleteTask(task.id)}}>X</button></li>; | |
}); | |
return (<ul> | |
{tasks} | |
</ul>); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment