This repository has been archived by the owner on May 20, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0366e6
commit 69dbbca
Showing
4 changed files
with
94 additions
and
78 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,74 @@ | ||
import React from 'react'; | ||
import { createStore, useStore } from '../src'; | ||
|
||
// this one is more complex, it has a name and a reducer function | ||
createStore( | ||
'todoList', | ||
{ | ||
idCount: 0, | ||
todos: [{ id: 0, text: 'buy milk' }], | ||
}, | ||
(state, action) => { | ||
// when a reducer is being used, you must return a new state object | ||
switch (action.type) { | ||
case 'create': | ||
const id = ++state.idCount; | ||
return { | ||
...state, | ||
todos: [ | ||
...state.todos, | ||
{ id, text: action.payload } | ||
] | ||
} | ||
case 'delete': | ||
return { | ||
...state, | ||
todos: state.todos.filter(todo => todo.id !== action.payload) | ||
} | ||
default: | ||
return state; | ||
} | ||
} | ||
); | ||
|
||
export function AddTodo() { | ||
const [ state, dispatch ] = useStore('todoList'); | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
const input = e.target.querySelector('input'); | ||
const todo = input.value; | ||
input.value = ''; | ||
dispatch({ type: 'create', payload: todo }); | ||
} | ||
|
||
return ( | ||
<form onSubmit={onSubmit}> | ||
<input></input> | ||
<button>Create TODO</button> | ||
</form> | ||
) | ||
} | ||
|
||
export function TodoList() { | ||
// Grab the correct store by specifying its namespace | ||
const [ state, dispatch ] = useStore('todoList'); | ||
const deleteTodo = id => dispatch({ type: 'delete', payload: id }) | ||
return ( | ||
<ul> | ||
<h2>TODOLIST</h2> | ||
{ state.todos.map(todo => | ||
<li key={todo.id}> | ||
{ todo.text } <button onClick={() => deleteTodo(todo.id)} type="button">X</button> | ||
</li>) | ||
} | ||
</ul> | ||
) | ||
} | ||
import React from 'react'; | ||
import { createStore, useStore } from '../src'; | ||
|
||
// this one is more complex, it has a name and a reducer function | ||
createStore( | ||
'todoList', | ||
{ | ||
idCount: 0, | ||
todos: [{ id: 0, text: 'buy milk' }], | ||
}, | ||
(state, action) => { | ||
// when a reducer is being used, you must return a new state object | ||
switch (action.type) { | ||
case 'create': | ||
const id = ++state.idCount; | ||
return { | ||
...state, | ||
todos: [ | ||
...state.todos, | ||
{ id, text: action.payload } | ||
] | ||
} | ||
case 'delete': | ||
return { | ||
...state, | ||
todos: state.todos.filter(todo => todo.id !== action.payload) | ||
} | ||
default: | ||
return state; | ||
} | ||
} | ||
); | ||
|
||
export function AddTodo() { | ||
const [ state, dispatch ] = useStore('todoList'); | ||
let input; | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
input = e.target.querySelector('input'); | ||
const todo = input.value; | ||
input.value = '.............'; | ||
input.disabled = true; | ||
dispatch({ type: 'create', payload: todo },todoCreated); | ||
} | ||
|
||
const todoCreated=(newState)=>{ | ||
input.disabled = false; | ||
input.value = ''; | ||
} | ||
|
||
return ( | ||
<form onSubmit={onSubmit}> | ||
<input></input> | ||
<button>Create TODO</button> | ||
</form> | ||
) | ||
} | ||
|
||
export function TodoList() { | ||
// Grab the correct store by specifying its namespace | ||
const [ state, dispatch ] = useStore('todoList'); | ||
const deleteTodo = id => dispatch({ type: 'delete', payload: id }) | ||
return ( | ||
<ul> | ||
<h2>TODOLIST</h2> | ||
{ state.todos.map(todo => | ||
<li key={todo.id}> | ||
{ todo.text } <button onClick={() => deleteTodo(todo.id)} type="button">X</button> | ||
</li>) | ||
} | ||
</ul> | ||
) | ||
} |
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