React State Helpers
Installation
yarn add react-state-helpers
Higher Order Component Wrapper
You can add react-state-helpers
to any project quickly and easily with the supplied decorator.
;; // login could be a function that takes on object with the keys:// userName, and password; @wrapStateHelpers { thisprops; } { const handleSubmit mut values: userName } = thisprops; return <div> Welcome userName ! <form onSubmit=> <input name='userName' type='text' onChange=/> <input name='password' type='password' /> <input type='submit' value='Login' /> </form> </div> }
If you are on an older version of Javascript you can use the following syntax for the same results...
;; // ... methods are the sameExample;
Mut Usage
Arguments:
string (string)
: The name of the mutating property as it appears in the component state.
[function = () => {}] (function)
: A preprocessing function
;; @wrapStateHelpers { // mut is a part of the props that wrapStateHelpers brings in. const props: mut values: someKey someNumber } = this; return <input type='text' value=someKey onChange= /> <input type='number' value=someNumber onChange= /> ; }}
Toggle Usage
Arguments:
string (string)
: The name of property to be toggled as it appears in the component state.
;;; // external package @wrapStateHelpers { const props: toggle values: showModal } = this; return <div> <Button onClick=>Open</Button> <Modal isOpen=showModal> <Button onClick=>'Cancel'</Button> </Modal> </div> ; }}
HandleSubmit Usage
Arguments:
function (function)
: The handler function for submitting a form.
;;; const mapStateToProps = state;const mapDispatchToProps = { ; }; @@wrapStateHelpers { const login handleSubmit = thisprops; const submit = ; return <form onSubmit=> <input name='username' type='text' /> <input name='password' type='password' /> <button type='submit'>Login</button> </form> ; }
Shorthand Redux
With using redux as the source of data, the first paramater of mut
becomes irrelevant.
Arguments:
function (function)
: A redux action that accepts the value of the input as a parameter
;;;;; ; const mapStateToProps = someKey: statesomewheresomeKey; const mapDispatchToProps = setSomeKey: ; // Chaining decorators is super easy!@@wrapStateHelpers static propTypes = someKey: PropTypesstring { const someKey setSomeKey withValue = thisprops; return <input type='text' value=someKey onChange= /> ; }
Available Functions
These functions can be found in your components props after using wrapStateHelpers
- findValue
- takes an event or value and returns the value.
- useful, if you want a common interface for handling events.
- ex:{const value = ;this;}// in render...<input value=someKey onChange=handleChange />
- ex:
- mut
- provides a short-hand for setting a state value.
- toggle
- set a value in the state to its inverse.
- handleSubmit
- creates a helper that will pass in all form values to a callback function.
Want to stop using redux-forms?
Most components don't need to use redux-forms, as many inputs don't need to change the state every onChange
, onKeyUp
, onKeyDown
etc.
Before
;// ... in render<Field name='firstName' component='input' type='text' /><Field name='lastName' component='input' type='text' />// ... form: 'formname' MyComponent;
After
no reliance on redux!
;// ... before class declaration@wrapStateHelpers// ... in renderconst props: mut state: firstName lastName } = this;// ... still in render<input value=firstName onChange= type='text' /><input value=lasteName onChange= type='text' />