This repository was archived by the owner on Dec 29, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import React , { useState } from "react" ;
2- import { useRex } from "rex-state" ;
2+ import store from "./store/store" ;
3+
4+ const { useRex } = store ;
35
46export default function App ( ) {
57 const store = useRex ( ) ;
@@ -23,15 +25,13 @@ export default function App() {
2325 < button onClick = { onAddTask } > Submit</ button >
2426 < ul >
2527 { todos . list . map ( ( item , itemIndex ) => {
26- const onClickCheckbox = ( ) => {
27- // now how to toggle this elegantly??
28- } ;
28+ const onClickCheckbox = ( ) => todos . toggleTask ( itemIndex ) ;
2929 return (
3030 < li key = { itemIndex } >
3131 < input
3232 type = "checkbox"
3333 onChange = { onClickCheckbox }
34- checked = { item . taskStatus }
34+ checked = { item . status }
3535 />
3636 { item . task }
3737 </ li >
Original file line number Diff line number Diff line change 11import React from "react" ;
22import ReactDOM from "react-dom" ;
3- import { RexProvider } from "rex-state" ;
43import store from "./store/store" ;
54import App from "./App" ;
6- // import './index.css'
5+
6+ const { RexProvider } = store ;
77
88ReactDOM . render (
9- < RexProvider store = { store } >
9+ < RexProvider >
1010 < App />
1111 </ RexProvider > ,
1212 document . getElementById ( "app-root" )
Original file line number Diff line number Diff line change @@ -9,19 +9,22 @@ export interface ITodosRexState {
99 list : ITask [ ] ;
1010}
1111
12- class Todos extends Rex {
13- state = {
14- list : [
15- {
16- task : "Task One" ,
17- status : false
18- } ,
19- {
20- task : "Task Two" ,
21- status : false
22- }
23- ]
24- } ;
12+ class Todos extends Rex < ITodosRexState > {
13+ constructor ( ) {
14+ super ( ) ;
15+ this . state = {
16+ list : [
17+ {
18+ task : "Task One" ,
19+ status : false
20+ } ,
21+ {
22+ task : "Task Two" ,
23+ status : false
24+ }
25+ ]
26+ } ;
27+ }
2528
2629 addTask = ( newTask : string ) => {
2730 const newTaskObject = {
@@ -32,6 +35,15 @@ class Todos extends Rex {
3235 this . setState ( { list } ) ;
3336 } ;
3437
38+ toggleTask = ( index : number ) => {
39+ const list = [ ...this . state . list ] ;
40+ list [ index ] = {
41+ task : list [ index ] . task ,
42+ status : ! list [ index ] . status
43+ } ;
44+ this . setState ( { list } ) ;
45+ } ;
46+
3547 get list ( ) {
3648 return this . state . list ;
3749 }
Original file line number Diff line number Diff line change 11import Todos from "./Todos" ;
2+ import { createRexStore } from "rex-state" ;
23
3- const store = {
4+ const store = createRexStore ( {
45 todos : Todos
5- } ;
6+ } ) ;
67
78export default store ;
You can’t perform that action at this time.
0 commit comments