{"name":"mobx-react","version":"6.0.0-rc.1","description":"React bindings for MobX. Create fully reactive components.","source":"src/index.js","main":"dist/mobx-react.js","jsnext:main":"dist/mobx-react.module.js","umd:main":"dist/mobx-react.umd.js","unpkg":"dist/mobx-react.umd.js","module":"dist/mobx-react.module.js","react-native":"dist/mobx-react.module.js","types":"dist/mobx-react.d.ts","repository":{"type":"git","url":"git+https://github.com/mobxjs/mobx-react.git"},"scripts":{"prettier":"prettier --write \"**/*.js\" \"**/*.ts\"","test":"jest && npm run test:ts","test:ts":"tsc -p test/ts","test:travis":"npm run build && jest && npm run test:ts","build":"yarn bundle && shx cp src/index.d.ts dist/mobx-react.d.ts","bundle":"microbundle --external mobx,react,react-dom,react-native --globals react-dom=ReactDOM,react-native=ReactNative,react=React --name mobxReact","watch":"jest --watch"},"author":{"name":"Michel Weststrate"},"license":"MIT","bugs":{"url":"https://github.com/mobxjs/mobx/issues"},"homepage":"https://mobxjs.github.io/mobx","peerDependencies":{"mobx":"^4.0.0 || ^5.0.0","react":"^16.8.0"},"devDependencies":{"@babel/core":"^7.1.0","@babel/plugin-proposal-class-properties":"^7.1.0","@babel/plugin-proposal-decorators":"^7.1.0","@babel/plugin-transform-react-jsx":"^7.0.0","@babel/preset-env":"^7.1.0","@types/create-react-class":"^15.6.0","@types/node":"^10.0.0","@types/prop-types":"^15.5.2","@types/react":"^16.0.13","@types/react-dom":"^16.0.1","babel-jest":"^24.1.0","create-react-class":"^15.6.2","husky":"^1.0.0","jest":"^24.0.0","jest-environment-jsdom":"^24.0.0","lint-staged":"^7.0.5","lodash":"^4.17.4","microbundle":"^0.9.0","mobx":"^5.0.0","prettier":"^1.7.2","prop-types":"^15.6.0","react":"^16.8.2","react-dom":"^16.8.2","react-test-renderer":"^16.6.3","request":"^2.83.0","shelljs":"^0.8.3","shx":"^0.3.2","typescript":"^2.6.0"},"dependencies":{"mobx-react-lite":"1.1.0"},"keywords":["mobx","mobservable","react-component","react","reactjs","reactive"],"lint-staged":{"*.{ts,js}":["prettier --write","git add"]},"jest":{"testURL":"http://127.0.0.1/"},"husky":{"hooks":{"pre-commit":"lint-staged"}},"gitHead":"464035b1f0a5367a6550bb3b590de62facdead78","readme":"# mobx-react\n\n[![Build Status](https://travis-ci.org/mobxjs/mobx-react.svg?branch=master)](https://travis-ci.org/mobxjs/mobx-react)\n[![Join the chat at https://gitter.im/mobxjs/mobx](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mobxjs/mobx?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)\n[![CDNJS](https://img.shields.io/cdnjs/v/mobx-react.svg)](https://cdnjs.com/libraries/mobx-react)\n\nPackage with React component wrapper for combining React with MobX.\nExports the `observer` decorator and some development utilities.\nFor documentation, see the [MobX](https://mobxjs.github.io/mobx) project.\nThis package supports both React and React Native.\n\n## Choosing your version\n\nThere are currently two actively maintained versions of mobx-react:\n\n| NPM Version | Supported React versions | Supports hook based components |\n| --- | --- | -- |\n| v6 | 16.8.0 and higher | Yes |\n| v5 | 0.13 and higher | No, but it is possible to use `<Observer>` sections inside hook based components |\n\nThe V5 documentation can be found in the [README_v5](README_v5.md).\n\nVersion 6 is a repackage of the [mobx-react-lite](https://github.com/mobxjs/mobx-react-lite) package + following features from the `mobx-react@5` package added:\n* Support for class based components for `observer` and `@observer`\n* `Provider / inject` to pass stores around (but consider to use `React.createContext` instead)\n* `PropTypes` to describe observable based property checkers (but consider to use TypeScript instead)\n* The `disposeOnUnmount` utility / decorator to easily clean up resources such as reactions created in your class based components.\n\n## Installation\n\n`npm install mobx-react --save`\n\nOr CDN: https://unpkg.com/mobx-react (namespace: `mobxReact`)\n\n```javascript\nimport { observer } from \"mobx-react\"\n// - or, for custom renderers without DOM: -\nimport { observer } from \"mobx-react/custom\"\n```\n\nThis package provides the bindings for MobX and React.\nSee the [official documentation](http://mobxjs.github.io/mobx/intro/overview.html) for how to get started.\n\nIf you are using [React hooks](https://reactjs.org/docs/hooks-intro.html) with latest React 16.7 and you like living on the bleeding edge then have a look at the new [mobx-react-lite](https://github.com/mobxjs/mobx-react-lite).\n\n## API documentation\n\nPlease check [mobx.js.org](https://mobx.js.org) for the general documentation. The documentation below highlights some specifics.\n\n### observer(componentClass)\n\nFunction (and decorator) that converts a React component definition, React 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.\n\nApart from observables passed/injected in or defined inside an `observer` component, `this.props` and `this.state` are also observables themselves, so the component will react to all changes in props and state that are  used by `render`.\n\nSee the [MobX](https://mobxjs.github.io/mobx/refguide/observer-component.html) documentation for more details.\n\n```javascript\nimport { observer } from \"mobx-react\"\n\n// ---- ES5 syntax ----\n\nconst TodoView = observer(\n    React.createClass({\n        displayName: \"TodoView\",\n        render() {\n            return <div>{this.props.todo.title}</div>\n        }\n    })\n)\n\n// ---- ES6 syntax ----\n\nconst TodoView = observer(\n    class TodoView extends React.Component {\n        render() {\n            return <div>{this.props.todo.title}</div>\n        }\n    }\n)\n\n// ---- ESNext syntax with decorators ----\n\n@observer\nclass TodoView extends React.Component {\n    render() {\n        return <div>{this.props.todo.title}</div>\n    }\n}\n\n// ---- or just use function components: ----\n\nconst TodoView = observer(({ todo }) => <div>{todo.title}</div>)\n```\n\n### `Observer`\n\n`Observer` is a React component, which applies `observer` to an anonymous region in your component.\nIt takes as children a single, argumentless function which should return exactly one React component.\nThe rendering in the function will be tracked and automatically re-rendered when needed.\nThis can come in handy when needing to pass render function to external components (for example the React Native listview), or if you\ndislike the `observer` decorator / function.\n\n```javascript\nclass App extends React.Component {\n    render() {\n        return (\n            <div>\n                {this.props.person.name}\n                <Observer>{() => <div>{this.props.person.name}</div>}</Observer>\n            </div>\n        )\n    }\n}\n\nconst person = observable({ name: \"John\" })\n\nReact.render(<App person={person} />, document.body)\nperson.name = \"Mike\" // will cause the Observer region to re-render\n```\n\nIn case you are a fan of render props, you can use that instead of children. Be advised, that you cannot use both approaches at once, children have a precedence.\nExample\n\n```javascript\nclass App extends React.Component {\n    render() {\n        return (\n            <div>\n                {this.props.person.name}\n                <Observer render={() => <div>{this.props.person.name}</div>} />\n            </div>\n        )\n    }\n}\n\nconst person = observable({ name: \"John\" })\n\nReact.render(<App person={person} />, document.body)\nperson.name = \"Mike\" // will cause the Observer region to re-render\n```\n\n\n### Server Side Rendering with `useStaticRendering`\n\nWhen using server side rendering, normal lifecycle hooks of React components are not fired, as the components are rendered only once.\nSince components are never unmounted, `observer` components would in this case leak memory when being rendered server side.\nTo avoid leaking memory, call `useStaticRendering(true)` when using server side rendering.\n\n```javascript\nimport { useStaticRendering } from \"mobx-react\"\n\nuseStaticRendering(true);\n```\n\nThis makes sure the component won't try to react to any future data changes.\n\n### Which components should be marked with `observer`?\n\nThe simple rule of thumb is: _all components that render observable data_.\nIf you don't want to mark a component as observer, for example to reduce the dependencies of a generic component package, make sure you only pass it plain data.\n\n### Enabling decorators (optional)\n\nDecorators are currently a stage-2 ESNext feature. How to enable them is documented [here](https://github.com/mobxjs/mobx#enabling-decorators-optional).\n\n### Should I still use smart and dumb components?\n\nSee this [thread](https://www.reddit.com/r/reactjs/comments/4vnxg5/free_eggheadio_course_learn_mobx_react_in_30/d61oh0l).\nTL;DR: the conceptual distinction makes a lot of sense when using MobX as well, but use `observer` on all components.\n\n### `PropTypes`\n\nMobX-react provides the following additional `PropTypes` which can be used to validate against MobX structures:\n\n*   `observableArray`\n*   `observableArrayOf(React.PropTypes.number)`\n*   `observableMap`\n*   `observableObject`\n*   `arrayOrObservableArray`\n*   `arrayOrObservableArrayOf(React.PropTypes.number)`\n*   `objectOrObservableObject`\n\nUse `import { PropTypes } from \"mobx-react\"` to import them, then use for example `PropTypes.observableArray`\n\n### `Provider` and `inject`\n\n`Provider` is a component that can pass stores (or other stuff) using React's context mechanism to child components.\nThis is useful if you have things that you don't want to pass through multiple layers of components explicitly.\n\n`inject` can be used to pick up those stores. It is a higher order component that takes a list of strings and makes those stores available to the wrapped component.\n\nExample (based on the official [context docs](https://facebook.github.io/react/docs/context.html#passing-info-automatically-through-a-tree)):\n\n```javascript\n@inject(\"color\")\n@observer\nclass Button extends React.Component {\n    render() {\n        return <button style={{ background: this.props.color }}>{this.props.children}</button>\n    }\n}\n\nclass Message extends React.Component {\n    render() {\n        return (\n            <div>\n                {this.props.text} <Button>Delete</Button>\n            </div>\n        )\n    }\n}\n\nclass MessageList extends React.Component {\n    render() {\n        const children = this.props.messages.map(message => <Message text={message.text} />)\n        return (\n            <Provider color=\"red\">\n                <div>{children}</div>\n            </Provider>\n        )\n    }\n}\n```\n\nNotes:\n\n*   If a component asks for a store and receives a store via a property with the same name, the property takes precedence. Use this to your advantage when testing!\n*   Values provided through `Provider` should be final, to avoid issues like mentioned in [React #2517](https://github.com/facebook/react/issues/2517) and [React #3973](https://github.com/facebook/react/pull/3973), where optimizations might stop the propagation of new context. Instead, make sure that if you put things in `context` that might change over time, that they are `@observable` or provide some other means to listen to changes, like callbacks. However, if your stores will change over time, like an observable value of another store, MobX will warn you. To suppress that warning explicitly, you can use `suppressChangedStoreWarning={true}` as a prop at your own risk.\n*   When using both `@inject` and `@observer`, make sure to apply them in the correct order: `observer` should be the inner decorator, `inject` the outer. There might be additional decorators in between.\n*   The original component wrapped by `inject` is available as the `wrappedComponent` property of the created higher order component.\n\n#### Inject as function\n\nThe above example in ES5 would start like:\n\n```javascript\nvar Button = inject(\"color\")(\n    observer(\n        React.createClass({\n            /* ... etc ... */\n        })\n    )\n)\n```\n\nA functional stateless component would look like:\n\n```javascript\nvar Button = inject(\"color\")(\n    observer(({ color }) => {\n        /* ... etc ... */\n    })\n)\n```\n\n#### Customizing inject\n\nInstead of passing a list of store names, it is also possible to create a custom mapper function and pass it to inject.\nThe mapper function receives all stores as argument, the properties with which the components are invoked and the context, and should produce a new set of properties,\nthat are mapped into the original:\n\n`mapperFunction: (allStores, props, context) => additionalProps`\n\nSince version 4.0 the `mapperFunction` itself is tracked as well, so it is possible to do things like:\n\n```javascript\nconst NameDisplayer = ({ name }) => <h1>{name}</h1>\n\nconst UserNameDisplayer = inject(stores => ({\n    name: stores.userStore.name\n}))(NameDisplayer)\n\nconst user = mobx.observable({\n    name: \"Noa\"\n})\n\nconst App = () => (\n    <Provider userStore={user}>\n        <UserNameDisplayer />\n    </Provider>\n)\n\nReactDOM.render(<App />, document.body)\n```\n\n_N.B. note that in this *specific* case neither `NameDisplayer` nor `UserNameDisplayer` needs to be decorated with `observer`, since the observable dereferencing is done in the mapper function_\n\n#### Using `propTypes` and `defaultProps` and other static properties in combination with `inject`\n\nInject wraps a new component around the component you pass into it.\nThis means that assigning a static property to the resulting component, will be applied to the HoC, and not to the original component.\nSo if you take the following example:\n\n```javascript\nconst UserName = inject(\"userStore\")(({ userStore, bold }) => someRendering())\n\nUserName.propTypes = {\n    bold: PropTypes.boolean.isRequired,\n    userStore: PropTypes.object.isRequired // will always fail\n}\n```\n\nThe above propTypes are incorrect, `bold` needs to be provided by the caller of the `UserName` component and is checked by React.\nHowever, `userStore` does not need to be required! Although it is required for the original stateless function component, it is not\nrequired for the resulting inject component. After all, the whole point of that component is to provide that `userStore` itself.\n\nSo if you want to make assertions on the data that is being injected (either stores or data resulting from a mapper function), the propTypes\nshould be defined on the _wrapped_ component. Which is available through the static property `wrappedComponent` on the inject component:\n\n```javascript\nconst UserName = inject(\"userStore\")(({ userStore, bold }) => someRendering())\n\nUserName.propTypes = {\n    bold: PropTypes.boolean.isRequired // could be defined either here ...\n}\n\nUserName.wrappedComponent.propTypes = {\n    // ... or here\n    userStore: PropTypes.object.isRequired // correct\n}\n```\n\nThe same principle applies to `defaultProps` and other static React properties.\nNote that it is not allowed to redefine `contextTypes` on `inject` components (but is possible to define it on `wrappedComponent`)\n\nFinally, mobx-react will automatically move non React related static properties from wrappedComponent to the inject component so that all static fields are\nactually available to the outside world without needing `.wrappedComponent`.\n\n#### Strongly typing inject\n\n##### With TypeScript\n\n`inject` also accepts a function (`(allStores, nextProps, nextContext) => additionalProps`) that can be used to pick all the desired stores from the available stores like this.\nThe `additionalProps` will be merged into the original `nextProps` before being provided to the next component.\n\n```typescript\nimport { IUserStore } from \"myStore\"\n\n@inject(allStores => ({\n    userStore: allStores.userStore as IUserStore\n}))\nclass MyComponent extends React.Component<{ userStore?: IUserStore; otherProp: number }, {}> {\n    /* etc */\n}\n```\n\nMake sure to mark `userStore` as an optional property. It should not (necessarily) be passed in by parent components at all!\n\nNote: If you have strict null checking enabled, you could muffle the nullable type by using the `!` operator:\n\n```\npublic render() {\n   const {a, b} = this.store!\n   // ...\n}\n```\n\n#### Testing store injection\n\nIt is allowed to pass any declared store in directly as a property as well. This makes it easy to set up individual component tests without a provider.\n\nSo if you have in your app something like:\n\n```javascript\n<Provider profile={profile}>\n    <Person age={\"30\"} />\n</Provider>\n```\n\nIn your test you can easily test the `Person` component by passing the necessary store as prop directly:\n\n```\nconst profile = new Profile()\nconst mountedComponent = mount(\n   <Person age={'30'} profile={profile} />\n)\n```\n\nBear in mind that using shallow rendering won't provide any useful results when testing injected components; only the injector will be rendered.\nTo test with shallow rendering, instantiate the `wrappedComponent` instead: `shallow(<Person.wrappedComponent />)`\n\n### disposeOnUnmount(componentInstance, propertyKey | function | function[])\n\nFunction (and decorator) that makes sure a function (usually a disposer such as the ones returned by `reaction`, `autorun`, etc.) is automatically executed as part of the componentWillUnmount lifecycle event.\n\n```javascript\nimport { disposeOnUnmount } from \"mobx-react\"\n\nclass SomeComponent extends React.Component {\n    // decorator version\n    @disposeOnUnmount\n    someReactionDisposer = reaction(...)\n\n    // function version over properties\n    someReactionDisposer = disposeOnUnmount(this, reaction(...))\n\n    // function version inside methods\n    componentDidMount() {\n        // single function\n        disposeOnUnmount(this, reaction(...))\n\n        // or function array\n        disposeOnUnmount(this, [\n            reaction(...),\n            reaction(...)\n        ])\n    }\n}\n```\n\n## DevTools\n\n`mobx-react@6` and higher are no longer compatible with the mobx-react-devtools. \nThat is, the MobX react devtools will no longer show render timings or dependency trees of the component. \nThe reason is that the standard React devtools are no also capable of highlighting re-rendering components.\nAnd the dependency tree of a component can now be inspected by the standard devtools as well, as shown in the image below:\n\n![hooks.png](hooks.png)\n\nThe event spy will still work as is.\n\n## FAQ\n\n**Should I use `observer` for each component?**\n\nYou should use `observer` on every component that displays observable data.\nEven the small ones. `observer` allows components to render independently from their parent and in general this means that\nthe more you use `observer`, the better the performance become.\nThe overhead of `observer` itself is negligible.\nSee also [Do child components need `@observer`?](https://github.com/mobxjs/mobx/issues/101)\n\n**I see React warnings about `forceUpdate` / `setState` from React**\n\nThe following warning will appear if you trigger a re-rendering between instantiating and rendering a component:\n\n```\nWarning: forceUpdate(...): Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.`\n```\n\n-- or --\n\n```\nWarning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.\n```\n\nUsually this means that (another) component is trying to modify observables used by this components in their `constructor` or `getInitialState` methods.\nThis violates the React Lifecycle, `componentWillMount` should be used instead if state needs to be modified before mounting.\n","readmeFilename":"README.md","_id":"mobx-react@6.0.0-rc.1","_npmVersion":"6.4.1","_nodeVersion":"10.15.1","_npmUser":{"name":"mweststrate","email":"mweststrate@gmail.com"},"dist":{"integrity":"sha512-W6Z6Joy9QlViniEWwwgChlE9t8jyews4nHd4iBmhD6h3ArXLrx8IiUufzSo6BeKR76i28y+x+5fp51lt94OuiA==","shasum":"46a754276d5275b6bcfedfe1e76fe5e04d6b5118","tarball":"http://registry-npm.ywwl.com/mobx-react/download/mobx-react-6.0.0-rc.1.tgz","fileCount":13,"unpackedSize":281094,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJck48hCRA9TVsSAnZWagAA/ncP/RBpyc2qxmmeLCE5kRfi\n3IuIv6/G3mkoctdU1kLVHPGPOaSPPV1/O7pQf3nmGvWUtbZ8clgXpLoVA+UP\n6KUq3ufrYLwrwBP+J7YZ3byVPJDHcFdo9eHRRiy/tv3DlHg+puYJkJjWC0Uc\nINlfwFjXM7cnyVbA0dmvqr+xIr3RtnznH2UzKPrnYbLObP64XfBkvu3AM21m\nKpZZo/phYMcoOyuDSQDFkHIU1Zw1k+9v1WRp156eMTyKj1zqMV/+b63OHxSd\nQOjta8e8PSCBlhRnfnrY8mIT+ByWiE16e7lxV6+B5WKfOlxwSGU3Vqmq2Mxy\n5xebn7WRFA/zKLk1BO1R2iy3gG4EPDtmwO1S7n5syHbwQnbxFnQQmPZUzhFr\nsbhRpumudg1njcf8Z+EHYGC5iw5+8xgdxkE+bKPxqcPeBUERfjrNa2NGkJlc\nvx+dnRksiwYVZhkduClpGSBLIUoPmWTcjHOUydqP2dHX9Vr8WFn0LYJhMD4z\ndVQz6ecGDbLYUNk1n2l/D0h4apomg6RkI5hRR6RITSId7cB510B1fdwAJjZb\npTcGPuwnGkiKv+hBfuMT4sM3sCo1yAp8f+DCxfhP5z3y4YQw0XwhLFogSzxg\nxrg3jlwKkCKrTu59OgXI0eSgiZSy1AYyBksMSdcLjLIErqmACRn9PO4pVIoL\nIYPo\r\n=jCVT\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDfym5C3/KxthkbiXuIYcnoUwNYDR0Wkeg7rMg4qAntewIgYrc6b2AxeOm27cChKQxHKjeq6g83CkUjWDehDfL/ryw="}],"size":85053,"noattachment":false,"key":"/mobx-react/-/mobx-react-6.0.0-rc.1.tgz"},"maintainers":[{"email":"mail@andykog.com","name":"andykog"},{"email":"strate@yandex.ru","name":"eshenbrener"},{"email":"mweststrate@gmail.com","name":"mweststrate"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mobx-react_6.0.0-rc.1_1553174304154_0.04884088738365211"},"_hasShrinkwrap":false,"publish_time":1553174304405,"_cnpm_publish_time":1553174304405,"dist-tags":{"latest":"10.0.3"}}