Mobx Preact Lite
This is an adaptation of the observer
module (including all relevant tests) from
Mobx Preact,
made specifically for Preact with some features stripped out:
- Dev tools integration
- Error reporting
- Render reporting
Provider/inject features are also not implemented (yet).
If you would like to use this module with any of these missing features re-integrated, please create an issue or PR.
Installation
npm install mobx-preact-lite --save
;
API documentation
Docs are adapted from Mobx Preact
observer(componentClass)
Function (and decorator) that converts a Preact component class or
stand-alone render function into a reactive component, which tracks
which observables are used by render
and automatically re-renders the
component when one of these values changes.
See the MobX
documentation for more details.
; // ---- ES5 syntax ---- const TodoView = ; // ---- ES6 syntax ---- const TodoView = // ---- ESNext syntax with decorators ---- @observer Component { return <div>thispropstodotitle</div> } // ---- or just use a stateless component function: ---- const TodoView =
Observer
Observer
is a Preact component, which applies observer
to an anonymous region in your component.
It takes as children a single, argumentless function which should return exactly one React component.
The rendering in the function will be tracked and automatically re-rendered when needed.
This can come in handy when needing to pass render function to external components (for example the React Native listview), or if you
dislike the observer
decorator / function.
Example:
{ return <div> thispropspersonname <Observer> <div>thispropspersonname</div> </Observer> </div> } const person = Reactpersonname = "Mike" // will cause the Observer region to re-render
useStaticRendering
Server Side Rendering with When using server side rendering, normal lifecycle hooks of PReact components are not fired, as the components are rendered only once.
Since components are never unmounted, observer
components would in this case leak memory when being rendered server side.
To avoid leaking memory, call useStaticRendering(true)
when using server side rendering. This makes sure the component won't try to react to any future data changes.