Created
February 13, 2017 20:34
-
-
Save tops/bc5264c2f80f3e4db4b139fa6cf017b9 to your computer and use it in GitHub Desktop.
Simple React Demo - Todo-list
This file contains hidden or 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
| <?php | |
| $db = mysqli_connect("localhost","root","root","todo"); | |
| mysqli_query($db,"SET NAMES utf8"); | |
| $query = "INSERT INTO items (title) VALUES ('{$_GET['title']}')"; | |
| $result = mysqli_query($db, $query); | |
| echo json_encode(["id" => mysqli_insert_id($db), "title" => $_GET['title']]); |
This file contains hidden or 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
| <?php | |
| $db = mysqli_connect("localhost","root","root","todo"); | |
| mysqli_query($db,"SET NAMES utf8"); | |
| $query = "SELECT * FROM items"; | |
| $result = mysqli_query($db, $query); | |
| while($item = mysqli_fetch_assoc($result)){ | |
| $return[] = $item; | |
| } | |
| echo json_encode($return); |
This file contains hidden or 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
| <?php | |
| $db = mysqli_connect("localhost","root","root","todo"); | |
| mysqli_query($db,"SET NAMES utf8"); | |
| $query = "DELETE FROM items WHERE id = {$_GET['delete']}"; | |
| $result = mysqli_query($db, $query); |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="https://fb.me/react-15.1.0.js"></script> | |
| <script src="https://fb.me/react-dom-15.1.0.js"></script> | |
| <script src="https://fb.me/JSXTransformer-0.13.3.js"></script> | |
| <script src="https://npmcdn.com/axios/dist/axios.min.js"></script> | |
| </head> | |
| <body> | |
| <div id="container"> | |
| </div> | |
| <script type='text/jsx' src="script.jsx"></script> | |
| </body> | |
| </html> |
This file contains hidden or 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
| class App extends React.Component { | |
| constructor(props){ | |
| super(props); | |
| this.state = {}; | |
| } | |
| render(){ | |
| return ( | |
| <div> | |
| <TodoList name={this.state.name} /> | |
| </div> | |
| ); | |
| } | |
| } | |
| class AddTodoForm extends React.Component { | |
| render(){ | |
| return ( | |
| <div> | |
| <input type="text" ref={ (input) => {this.newTitle = input} } /> | |
| <input type="button" value="+" onClick={() => {this.props.addtodo(this.newTitle.value); this.newTitle.value = "";} } /> | |
| </div> | |
| ); | |
| } | |
| } | |
| class TodoList extends React.Component { | |
| constructor(props){ | |
| super(props); | |
| this.state = { | |
| items: [{title: 'Loading...'}] | |
| }; | |
| } | |
| componentDidMount(){ | |
| axios.get("api.php").then( | |
| (response) => { | |
| console.log(response.data); | |
| this.setState({items: response.data}); | |
| } | |
| ); | |
| } | |
| render(){ | |
| return ( | |
| <div> | |
| <AddTodoForm addtodo={this.addTodo.bind(this)} /> | |
| <ul> | |
| {this.state.items.map((item, key) => <TodoItem name={item.title} id={item.id} remove={() => this.removeItem(key, item.id)} />)} | |
| </ul> | |
| </div> | |
| ); | |
| } | |
| removeItem(key, id){ | |
| axios.delete("delete.php?delete="+id).then( | |
| (response) => { | |
| console.log(response.data); | |
| this.state.items.splice(key,1); | |
| this.setState({items: this.state.items}); | |
| // this.setState({items: response.data}); | |
| } | |
| ); | |
| } | |
| addTodo(title){ | |
| axios.post("add.php?title="+title).then( | |
| (response) => { | |
| this.state.items.push(response.data); | |
| this.setState({items: this.state.items}); | |
| } | |
| ); | |
| } | |
| } | |
| const TodoItem = ({name, id, remove}) => { | |
| return (<li onClick={remove}>{name} ({id})</li>); | |
| } | |
| ReactDOM.render( | |
| <App /> | |
| , | |
| document.getElementById('container') | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment