Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit fb01cce

Browse files
committed
updated example project
1 parent 4246fca commit fb01cce

4 files changed

Lines changed: 36 additions & 23 deletions

File tree

rex-example/src/App.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { useState } from "react";
2-
import { useRex } from "rex-state";
2+
import store from "./store/store";
3+
4+
const { useRex } = store;
35

46
export 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>

rex-example/src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import { RexProvider } from "rex-state";
43
import store from "./store/store";
54
import App from "./App";
6-
// import './index.css'
5+
6+
const { RexProvider } = store;
77

88
ReactDOM.render(
9-
<RexProvider store={store}>
9+
<RexProvider>
1010
<App />
1111
</RexProvider>,
1212
document.getElementById("app-root")

rex-example/src/store/Todos.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff 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
}

rex-example/src/store/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Todos from "./Todos";
2+
import { createRexStore } from "rex-state";
23

3-
const store = {
4+
const store = createRexStore({
45
todos: Todos
5-
};
6+
});
67

78
export default store;

0 commit comments

Comments
 (0)