@@ -19,8 +19,9 @@ npm install --save @bit-about/event
1919- 100% ** Idiomatic React**
2020- 100% Typescript with event types deduction
2121- Efficient and hook-based
22+ - ...with static listener and dispatcher
2223- No centralized event provider
23- - Tiny - only ** 100KB **
24+ - Tiny - only ** 2kB **
2425- ** Just works** ™
2526
2627## Usage
@@ -40,7 +41,7 @@ const [EventProvider, useEvent] = events({
4041``` jsx
4142const App = () => (
4243 < EventProvider>
43- { /* ... */ }
44+ ...
4445 < / EventProvider>
4546)
4647```
@@ -51,7 +52,7 @@ const App = () => (
5152const Button = () => {
5253 const dispatchEvent = useEvent ()
5354
54- const onButtonClick = () => dispatchEvent (' buttonClicked' , " Hello" )
55+ const onButtonClick = () => dispatchEvent (' buttonClicked' , ' Hello' )
5556
5657 return < button onClick= {onButtonClick}> Call event < / button>
5758}
@@ -60,7 +61,7 @@ const Button = () => {
6061👂 Listen on your events
6162``` jsx
6263const Component = () => {
63- const [message , setMessage ] = React .useState (" " )
64+ const [message , setMessage ] = React .useState (' ' )
6465
6566 useEvent ({
6667 buttonClicked : (payload : string ) => setMessage (payload)
@@ -70,26 +71,41 @@ const Component = () => {
7071}
7172```
7273
73- > ** You don't need to think too much** - it's easy, look:<br />
74- > - define events with payloads using ` events() ` <br />
75- > - wrap the components tree with the generated ` EventProvider ` <br />
76- > - listen on events with ** useEvent hook**
77- > - dispatch events with ** useEvent hook**
74+ ## Static access
75+ The third element of the ` events() ` result tuple is object which provides access in static manner (without hook).
76+
77+ ``` jsx
78+ const [AppEventProvider , useAppEvent , appEvent ] = events (... )
79+ ```
80+
81+ and then
82+ ``` jsx
83+ // 💪 Get substate
84+ appEvent .dispatch (' buttonClicked' , ' Hello Allice!' )
85+
86+ // 🤌 Subscribe and listen on new events
87+ const subscriber = appEvent .subscribe ({
88+ buttonClicked : (payload : string ) => console .log (payload)
89+ })
90+
91+ // remember to unsubscribe!
92+ subscriber .unsubscribe ()
93+ ```
7894
7995## 👉 Rerendering
8096Neither listeners nor event dispatching rerender the component.<br />
8197The component will only be rerendered if its state is explicitly changed (in e.g. ` React.useState ` ).
8298
8399``` jsx
84100const Component = () => {
85- const [message , setMessage ] = React .useState (" " )
101+ const [message , setMessage ] = React .useState (' ' )
86102
87103 useEvent ({
88- aliceClicked : () => console .log (" I DON'T rerender this component!" ),
89- bobClicked : () => setMessage (" I DO rerender this component!" )
104+ aliceClicked : () => console .log (' I DO NOT rerender this component!' ),
105+ bobClicked : () => setMessage (' I DO rerender this component!' )
90106 })
91107
92- // ...
108+ return < p > {message} < / p >
93109}
94110```
95111
@@ -98,52 +114,21 @@ Events in `events()` are actually payload middlewares.
98114
99115``` jsx
100116const [EventProvider , useEvent ] = events ({
101- buttonClicked : (payload : string ) => ` Hello ${ message} !` ,
117+ buttonClicked : (payload : string ) => ` Hello ${ message} !` , // Transforms string payload to another
118+ avatarClicked : () => ` Bob!` , // Provides default payload
102119})
103- ```
104120
105- ``` jsx
106- const dispatchEvent = useEvent ({
107- buttonClicked : (payload : string ) => console .log (payload) // "Hello Alice !"
121+ const dispatch = useEvent ({
122+ buttonClicked : ( payload : string ) => console . log (payload), // "Hello Alice!",
123+ avatarClicked : (payload : string ) => console .log (payload), // "Bob !"
108124})
109125
110- dispatchEvent (' buttonClicked' , " Alice" )
126+ dispatch (' buttonClicked' , ' Alice' )
127+ dispatch (' avatarClicked' )
111128```
112129
113130> NOTE: <br />
114- > The library is completely type safe <br />
115- > so Typescript will inform you when you use wrong payload anywhere
116-
117- #### Default payload
118- When you don't need the payload and want some default object, you can omit middleware parameters.
119- ``` jsx
120- const [EventProvider , useEvent ] = events ({
121- buttonClicked : () => ` Bob!` ,
122- })
123- ```
124-
125- ``` jsx
126- const dispatchEvent = useEvent ({
127- buttonClicked : (payload : string ) => console .log (payload) // "Bob!"
128- })
129-
130- dispatchEvent (' buttonClicked' )
131- ```
132-
133- #### Middleware helpers
134- Are you an aesthete? <br />
135- Try: ` withPayload<PayloadType>() ` , ` withDefault(defaultPayload) ` and ` withNothing ` .
136-
137- ``` tsx
138- const [EventProvider, useEvent] = events ({
139- userLogged: withPayload <{ id: number }>(),
140- homeVisited: withNothing ,
141- buttonClicked: withDefault ({ type: ' userButton' })
142- })
143- ```
144-
145- > NOTE:
146- > ` withPayload ` does the call with ` () ` . If you forget about it, your payload will be the function 🤡
131+ > The library is completely type safe so Typescript will inform you when you use wrong payload anywhere
147132
148133## BitAboutEvent 💛 [ BitAboutState] ( https://github.com/bit-about/state )
149134Are you tired of sending logic to the related components?<br />
0 commit comments