Svelte、Reactの比較~API処理~
1.
2. import Todo from "./Todo.svelte"
3.
4. let todoList = [];
5. let value = "";
6.
7. // APIモック
8.
9. const addTodo = () => {
10. todoList = [...todoList, value];
11. value = "";
12. }
13.
14. const deleteTodo = (index) => {
15. todoList.splice(index, 1);
16. todoList = todoList
17. }
18.
19.
20.
21. {#await fetchTodoList
()}
22.
Loading...
23. {:then _}
24.
25. {/await}
26.
1. import { useEffect , useState } from 'react' ;
2. import Todo from './Todo' ;
3.
4. function TodoContainer () {
5. const [todoList , setTodoList ] = useState ([]);
6. const [val, setVal ] = useState ("");
7.
8. // API モック
9.
10. useEffect (() => {
11. fetchTodoList ();
12. },[]);
13.
14. const addTodo = () => {
15. setTodoList ([... todoList , val]);
16. setVal ("");
17. }
18.
19. const deleteTodo = (index ) => {
20. const newTodoList = [... todoList ];
21. newTodoList .splice (index , 1);
22. setTodoList (newTodoList )
23. }
24.
25. if(!todoList .length ) return
loading...
26.
27. return (
28. <>
29.
30. >
31. );
32. }
33.
34. export default TodoContainer ;
App.js Todo
TodoContainer
App.svelte Todo
TodoContainer
31
PromiseのPending中
useEffectの処理前