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

Commit 863f13d

Browse files
committed
updated Readme
1 parent 2e2f01a commit 863f13d

1 file changed

Lines changed: 36 additions & 49 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

0 commit comments

Comments
 (0)