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

Commit 4012114

Browse files
committed
Merge branch 'rex-perf-fix'
2 parents 4dbf1fe + 863f13d commit 4012114

8 files changed

Lines changed: 177 additions & 202 deletions

File tree

README.md

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -50,66 +50,55 @@ npm i rex-state
5050

5151
Rex State is inspired by React's simplicity in building UI. Hence it borrows one of the most common React-ish style for creating & updating states.
5252

53-
### This is a traditional React Class Component
53+
### This is a classic React functional component with `useState` hook
5454

5555
```jsx
56-
import React, { Component } from "react";
57-
58-
export default class InputField extends Component {
59-
constructor() {
60-
super();
61-
this.state = {
62-
value: ""
63-
};
64-
}
65-
66-
updateValue = newValue => this.setState({ value: newValue });
67-
68-
render() {
69-
return (
70-
<input
71-
type="text"
72-
placeholder="Add Text here..."
73-
value={this.state.value}
74-
onChange={event => this.updateValue(event.target.value)}
75-
/>
76-
);
77-
}
78-
}
56+
import React, { useState } from "react";
57+
58+
const InputField = () => {
59+
const [value, updateValue] = useState("");
60+
61+
return (
62+
<input
63+
type="text"
64+
value={value}
65+
placeholder="Add Text here..."
66+
onChange={event => updateValue(event.target.value)}
67+
/>
68+
);
69+
};
7970
```
8071

81-
The above component will render a simple input field and will take care of updating the input state when a new value is entered in the input field. However, the state & UI are tightly coupled together and it is impossible to reuse the same state logic to a different UI.
72+
The above component will render a simple input field and will take care of updating the input state when a new value is entered in the input field. However, the state & UI are tightly coupled together and it is impossible to reuse the same state logic to a different component.
8273

8374
### This is the same component using Rex State
8475

8576
```jsx
8677
import React from "react";
87-
import Rex from "rex-state";
88-
89-
class InputState extends Rex {
90-
constructor() {
91-
super();
92-
this.state = {
93-
value: ""
94-
};
95-
}
96-
97-
get value() {
98-
return this.state.value;
99-
}
100-
101-
updateValue = newValue => this.setState({ value: newValue });
102-
}
78+
import useRex from "rex-state";
79+
80+
const useInput = () => {
81+
const [state, setState] = useRex({ value: "" });
82+
83+
return {
84+
get value() {
85+
return state.value;
86+
},
87+
updateValue(value: string) {
88+
setState({ value });
89+
}
90+
};
91+
};
10392

10493
const InputField = () => {
105-
const inputState = new InputState();
94+
const { value, updateValue } = useInput();
10695

10796
return (
10897
<input
10998
type="text"
99+
value={value}
110100
placeholder="Add Text here..."
111-
value={inputState.value}
112-
onChange={event => inputState.updateValue(event.target.value)}
101+
onChange={event => updateValue(event.target.value)}
113102
/>
114103
);
115104
};
@@ -119,15 +108,13 @@ export default InputField;
119108

120109
The functionality of the component remains unchanged, however we now have two entities.
121110

122-
- `InputState` is a class which is used to define your application state.
123-
- `InputField` is a functional React Component that uses `InputState` to render it's UI.
111+
- `useInput` is a hook which is used to define your application state.
112+
- `InputField` is a functional React Component that uses `useInput` hook to render it's UI.
124113

125-
This decouples the UI from the State and also provides a nice & familiar class based API to define & manage your states.
114+
This decouples the UI from the State and also provides a nice & familiar way to write an API to define & manage your states.
126115

127116
Refer the Example app. Documentation will be completed soon...
128117

129-
### You can also try it live in [**codesandbox!**](https://codesandbox.io/s/state-management-with-rex-4olpn?fontsize=14&hidenavigation=1&theme=dark)
130-
131118
## Running the Example App
132119

133120
- Clone this Repo

rex-example/src/App.tsx

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
import React, { useState } from "react";
2-
import store from "./store/store";
3-
4-
const { useRex } = store;
1+
import React from "react";
2+
import { useFormFieldStore } from "./store/useFormField";
53

64
export default function App() {
7-
const store = useRex();
8-
const { todos } = store;
9-
10-
const [newTask, updateNewTask] = useState("");
11-
12-
const onAddTask = () => {
13-
todos.addTask(newTask);
14-
updateNewTask("");
15-
};
16-
5+
const {
6+
values,
7+
updateName,
8+
updateEmail,
9+
updateAge,
10+
updatePhone
11+
} = useFormFieldStore();
1712
return (
1813
<div>
1914
<input
2015
type="text"
21-
value={newTask}
22-
onChange={event => updateNewTask(event.target.value)}
23-
placeholder="New Task"
16+
value={values.name}
17+
onChange={e => updateName(e.target.value)}
18+
placeholder="Name"
19+
/>
20+
<input
21+
type="text"
22+
value={values.email}
23+
onChange={e => updateEmail(e.target.value)}
24+
placeholder="Email"
25+
/>
26+
<input
27+
type="text"
28+
value={values.phone || ""}
29+
onChange={e => updatePhone(e.target.value)}
30+
placeholder="Phone"
31+
/>
32+
<input
33+
type="number"
34+
value={values.age || ""}
35+
onChange={e => updateAge(parseInt(e.target.value))}
36+
placeholder="Age"
2437
/>
25-
<button onClick={onAddTask}>Submit</button>
26-
<ul>
27-
{todos.list.map((item, itemIndex) => {
28-
const onClickCheckbox = () => todos.toggleTask(itemIndex);
29-
return (
30-
<li key={itemIndex}>
31-
<input
32-
type="checkbox"
33-
onChange={onClickCheckbox}
34-
checked={item.status}
35-
/>
36-
{item.task}
37-
</li>
38-
);
39-
})}
40-
</ul>
4138
</div>
4239
);
4340
}

rex-example/src/SimpleApp.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
import useRex from "rex-state";
3+
4+
const useInput = () => {
5+
const [state, setState] = useRex({ value: "" });
6+
7+
return {
8+
get value() {
9+
return state.value;
10+
},
11+
updateValue(value: string) {
12+
setState({ value });
13+
}
14+
};
15+
};
16+
17+
const SimpleApp = () => {
18+
const { value, updateValue } = useInput();
19+
20+
return (
21+
<input
22+
type="text"
23+
value={value}
24+
placeholder="Add Text here..."
25+
onChange={event => updateValue(event.target.value)}
26+
/>
27+
);
28+
};
29+
30+
export default SimpleApp;

rex-example/src/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import store from "./store/store";
43
import App from "./App";
4+
import { FormFieldProvider } from "./store/useFormField";
55

6-
const { RexProvider } = store;
6+
// import SimpleApp from "./SimpleApp";
7+
8+
// ReactDOM.render(<SimpleApp />, document.getElementById("app-root"));
79

810
ReactDOM.render(
9-
<RexProvider>
11+
<FormFieldProvider>
1012
<App />
11-
</RexProvider>,
13+
</FormFieldProvider>,
1214
document.getElementById("app-root")
1315
);

rex-example/src/store/Todos.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

rex-example/src/store/store.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import useRex, { createRexStore } from "rex-state";
2+
3+
type formFields = {
4+
name: string;
5+
email: string;
6+
gender?: "male" | "female";
7+
phone?: string;
8+
age?: number;
9+
};
10+
11+
const useFormField = () => {
12+
const defaultState: formFields = {
13+
name: "",
14+
email: ""
15+
};
16+
17+
const [state, setState] = useRex(defaultState);
18+
19+
return {
20+
get values() {
21+
return state;
22+
},
23+
updateName(name: formFields["name"]) {
24+
setState({ name });
25+
},
26+
updateEmail(email: formFields["email"]) {
27+
setState({ email });
28+
},
29+
updatePhone(phone: formFields["phone"]) {
30+
setState({ phone });
31+
},
32+
updateGender(gender: formFields["gender"]) {
33+
setState({ gender });
34+
},
35+
updateAge(age: formFields["age"]) {
36+
setState({ age });
37+
}
38+
};
39+
};
40+
41+
const { RexProvider, useStore } = createRexStore(useFormField);
42+
43+
export const FormFieldProvider = RexProvider;
44+
export const useFormFieldStore = useStore;

0 commit comments

Comments
 (0)