Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mobx-react

mobxjs5.5mMIT9.2.0TypeScript support: included

React bindings for MobX. Create fully reactive components.

mobx, mobservable, react-component, react, reactjs, reactive

readme

mobx-react

CircleCI CDNJS Minzipped size Discuss on Github View changelog

Package with React component wrapper for combining React with MobX. Exports the observer decorator and other utilities. For documentation, see the MobX project. This package supports both React and React Native.

Compatibility matrix

Only the latest version is actively maintained. If you're missing a fix or a feature in older version, consider upgrading or using patch-package

NPM Version Support MobX version Supported React versions Added support for:
v9 6.* >16.8 Hooks, React 18.2 in strict mode
v7 6.* >16.8 < 18.2 Hooks
v6 4.* / 5.* >16.8 <17 Hooks
v5 4.* / 5.* >0.13 <17 No, but it is possible to use <Observer> sections inside hook based components

mobx-react 6 / 7 is a repackage of the smaller mobx-react-lite package + following features from the mobx-react@5 package added:

  • Support for class based components for observer and @observer
  • Provider / inject to pass stores around (but consider to use React.createContext instead)
  • PropTypes to describe observable based property checkers (but consider to use TypeScript instead)
  • The disposeOnUnmount utility / decorator to easily clean up resources such as reactions created in your class based components.

Installation

npm install mobx-react --save

Or CDN: https://unpkg.com/mobx-react (UMD namespace: mobxReact)

import { observer } from "mobx-react"

This package provides the bindings for MobX and React. See the official documentation for how to get started.

For greenfield projects you might want to consider to use mobx-react-lite, if you intend to only use function based components. React.createContext can be used to pass stores around.

API documentation

Please check mobx.js.org for the general documentation. The documentation below highlights some specifics.

observer(component)

Function (and decorator) that converts a React component definition, React component class, or stand-alone render function, into a reactive component. A converted component will track which observables are used by its effective render and automatically re-render the component when one of these values changes.

Functional Components

React.memo is automatically applied to functional components provided to observer. observer does not accept a functional component already wrapped in React.memo, or an observer, in order to avoid consequences that might arise as a result of wrapping it twice.

Class Components

shouldComponentUpdate is not supported. As such, it is recommended that class components extend React.PureComponent. The observer will automatically patch non-pure class components with an internal implementation of React.PureComponent if necessary.

Extending observer class components is not supported. Always apply observer only on the last class in the inheritance chain.

See the MobX documentation for more details.

import { observer } from "mobx-react"

// ---- ES6 syntax ----
const TodoView = observer(
    class TodoView extends React.Component {
        render() {
            return <div>{this.props.todo.title}</div>
        }
    }
)

// ---- ESNext syntax with decorator syntax enabled ----
@observer
class TodoView extends React.Component {
    render() {
        return <div>{this.props.todo.title}</div>
    }
}

// ---- or just use function components: ----
const TodoView = observer(({ todo }) => <div>{todo.title}</div>)
Note on using props and state in derivations

mobx-react version 6 and lower would automatically turn this.state and this.props into observables. This has the benefit that computed properties and reactions were able to observe those. However, since this pattern is fundamentally incompatible with StrictMode in React 18.2 and higher, this behavior has been removed in React 18.

As a result, we recommend to no longer mark properties as @computed in observer components if they depend on this.state or this.props.

@observer
class Doubler extends React.Component<{ counter: number }> {
    @computed // BROKEN! <-- @computed should be removed in mobx-react > 7
    get doubleValue() {
        // Changes to this.props will no longer be detected properly, to fix it,
        // remove the @computed annotation.
        return this.props * 2
    }

    render() {
        return <div>{this.doubleValue}</div>
    }
}

Similarly, reactions will no longer respond to this.state / this.props. This can be overcome by creating an observable copy:

@observer
class Alerter extends React.Component<{ counter: number }> {
    @observable observableCounter: number
    reactionDisposer

    constructor(props) {
        this.observableCounter = counter
    }

    componentDidMount() {
        // set up a reaction, by observing the observable,
        // rather than the prop which is non-reactive:
        this.reactionDisposer = autorun(() => {
            if (this.observableCounter > 10) {
                alert("Reached 10!")
            }
        })
    }

    componentDidUpdate() {
        // sync the observable from props
        this.observableCounter = this.props.counter
    }

    componentWillUnmount() {
        this.reactionDisposer()
    }

    render() {
        return <div>{this.props.counter}</div>
    }
}

MobX-react will try to detect cases where this.props, this.state or this.context are used by any other derivation than the render method of the owning component and throw. This is to make sure that neither computed properties, nor reactions, nor other components accidentally rely on those fields to be reactive.

This includes cases where a render callback is passed to a child, that will read from the props or state of a parent component. As a result, passing a function that might later read a property of a parent in a reactive context will throw as well. Instead, when using a callback function that is being passed to an observer based child, the capture should be captured locally first:

@observer
class ChildWrapper extends React.Component<{ counter: number }> {
    render() {
        // Collapsible is an observer component that should respond to this.counter,
        // if it is expanded

        // BAD:
        return <Collapsible onRenderContent={() => <h1>{this.props.counter}</h1>} />

        // GOOD: (causes to pass down a fresh callback whenever counter changes,
        // that doesn't depend on its parents props)
        const counter = this.props.counter
        return <Collapsible onRenderContent={() => <h1>{counter}</h1>} />
    }
}

Observer

Observer is a React 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.

class App extends React.Component {
    render() {
        return (
            <div>
                {this.props.person.name}
                <Observer>{() => <div>{this.props.person.name}</div>}</Observer>
            </div>
        )
    }
}

const person = observable({ name: "John" })

ReactDOM.render(<App person={person} />, document.body)
person.name = "Mike" // will cause the Observer region to re-render

In 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. Example

class App extends React.Component {
    render() {
        return (
            <div>
                {this.props.person.name}
                <Observer render={() => <div>{this.props.person.name}</div>} />
            </div>
        )
    }
}

const person = observable({ name: "John" })

ReactDOM.render(<App person={person} />, document.body)
person.name = "Mike" // will cause the Observer region to re-render

useLocalObservable hook

Local observable state can be introduced by using the useLocalObservable hook, that runs once to create an observable store. A quick example would be:

import { useLocalObservable, Observer } from "mobx-react-lite"

const Todo = () => {
    const todo = useLocalObservable(() => ({
        title: "Test",
        done: true,
        toggle() {
            this.done = !this.done
        }
    }))

    return (
        <Observer>
            {() => (
                <h1 onClick={todo.toggle}>
                    {todo.title} {todo.done ? "[DONE]" : "[TODO]"}
                </h1>
            )}
        </Observer>
    )
}

When using useLocalObservable, all properties of the returned object will be made observable automatically, getters will be turned into computed properties, and methods will be bound to the store and apply mobx transactions automatically. If new class instances are returned from the initializer, they will be kept as is.

It is important to realize that the store is created only once! It is not possible to specify dependencies to force re-creation, nor should you directly be referring to props for the initializer function, as changes in those won't propagate.

Instead, if your store needs to refer to props (or useState based local state), the useLocalObservable should be combined with the useAsObservableSource hook, see below.

Note that in many cases it is possible to extract the initializer function to a function outside the component definition. Which makes it possible to test the store itself in a more straight-forward manner, and avoids creating the initializer closure on each re-render.

Note: using useLocalObservable is mostly beneficial for really complex local state, or to obtain more uniform code base. Note that using a local store might conflict with future React features like concurrent rendering.

Server Side Rendering with enableStaticRendering

When using server side rendering, normal lifecycle hooks of React 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 enableStaticRendering(true) when using server side rendering.

import { enableStaticRendering } from "mobx-react"

enableStaticRendering(true)

This makes sure the component won't try to react to any future data changes.

Which components should be marked with observer?

The simple rule of thumb is: all components that render observable data. If 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.

Enabling decorators (optional)

Decorators are currently a stage-2 ESNext feature. How to enable them is documented here.

Should I still use smart and dumb components?

See this thread. TL;DR: the conceptual distinction makes a lot of sense when using MobX as well, but use observer on all components.

PropTypes

MobX-react provides the following additional PropTypes which can be used to validate against MobX structures:

  • observableArray
  • observableArrayOf(React.PropTypes.number)
  • observableMap
  • observableObject
  • arrayOrObservableArray
  • arrayOrObservableArrayOf(React.PropTypes.number)
  • objectOrObservableObject

Use import { PropTypes } from "mobx-react" to import them, then use for example PropTypes.observableArray

Provider and inject

Note: usually there is no need anymore to use Provider / inject in new code bases; most of its features are now covered by React.createContext.

Provider is a component that can pass stores (or other stuff) using React's context mechanism to child components. This is useful if you have things that you don't want to pass through multiple layers of components explicitly.

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.

Example (based on the official context docs):

@inject("color")
@observer
class Button extends React.Component {
    render() {
        return <button style={{ background: this.props.color }}>{this.props.children}</button>
    }
}

class Message extends React.Component {
    render() {
        return (
            <div>
                {this.props.text} <Button>Delete</Button>
            </div>
        )
    }
}

class MessageList extends React.Component {
    render() {
        const children = this.props.messages.map(message => <Message text={message.text} />)
        return (
            <Provider color="red">
                <div>{children}</div>
            </Provider>
        )
    }
}

Notes:

  • It is possible to read the stores provided by Provider using React.useContext, by using the MobXProviderContext context that can be imported from mobx-react.
  • 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!
  • 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.
  • The original component wrapped by inject is available as the wrappedComponent property of the created higher order component.

"The set of provided stores has changed" error

Values provided through Provider should be final. 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 throw an error. This restriction exists mainly for legacy reasons. If you have a scenario where you need to modify the set of stores, please leave a comment about it in this issue https://github.com/mobxjs/mobx-react/issues/745. Or a preferred way is to use React Context directly which does not have this restriction.

Inject as function

The above example in ES5 would start like:

var Button = inject("color")(
    observer(
        class Button extends Component {
            /* ... etc ... */
        }
    )
)

A functional stateless component would look like:

var Button = inject("color")(
    observer(({ color }) => {
        /* ... etc ... */
    })
)

Customizing inject

Instead of passing a list of store names, it is also possible to create a custom mapper function and pass it to inject. The 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, that are mapped into the original:

mapperFunction: (allStores, props, context) => additionalProps

Since version 4.0 the mapperFunction itself is tracked as well, so it is possible to do things like:

const NameDisplayer = ({ name }) => <h1>{name}</h1>

const UserNameDisplayer = inject(stores => ({
    name: stores.userStore.name
}))(NameDisplayer)

const user = mobx.observable({
    name: "Noa"
})

const App = () => (
    <Provider userStore={user}>
        <UserNameDisplayer />
    </Provider>
)

ReactDOM.render(<App />, document.body)

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

Using PropTypes and defaultProps and other static properties in combination with inject

Inject wraps a new component around the component you pass into it. This means that assigning a static property to the resulting component, will be applied to the HoC, and not to the original component. So if you take the following example:

const UserName = inject("userStore")(({ userStore, bold }) => someRendering())

UserName.propTypes = {
    bold: PropTypes.boolean.isRequired,
    userStore: PropTypes.object.isRequired // will always fail
}

The above propTypes are incorrect, bold needs to be provided by the caller of the UserName component and is checked by React. However, userStore does not need to be required! Although it is required for the original stateless function component, it is not required for the resulting inject component. After all, the whole point of that component is to provide that userStore itself.

So if you want to make assertions on the data that is being injected (either stores or data resulting from a mapper function), the propTypes should be defined on the wrapped component. Which is available through the static property wrappedComponent on the inject component:

const UserName = inject("userStore")(({ userStore, bold }) => someRendering())

UserName.propTypes = {
    bold: PropTypes.boolean.isRequired // could be defined either here ...
}

UserName.wrappedComponent.propTypes = {
    // ... or here
    userStore: PropTypes.object.isRequired // correct
}

The same principle applies to defaultProps and other static React properties. Note that it is not allowed to redefine contextTypes on inject components (but is possible to define it on wrappedComponent)

Finally, mobx-react will automatically move non React related static properties from wrappedComponent to the inject component so that all static fields are actually available to the outside world without needing .wrappedComponent.

Strongly typing inject

With TypeScript

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. The additionalProps will be merged into the original nextProps before being provided to the next component.

import { IUserStore } from "myStore"

@inject(allStores => ({
    userStore: allStores.userStore as IUserStore
}))
class MyComponent extends React.Component<{ userStore?: IUserStore; otherProp: number }, {}> {
    /* etc */
}

Make sure to mark userStore as an optional property. It should not (necessarily) be passed in by parent components at all!

Note: If you have strict null checking enabled, you could muffle the nullable type by using the ! operator:

public render() {
   const {a, b} = this.store!
   // ...
}

Testing store injection

It 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.

So if you have in your app something like:

<Provider profile={profile}>
    <Person age={"30"} />
</Provider>

In your test you can easily test the Person component by passing the necessary store as prop directly:

const profile = new Profile()
const mountedComponent = mount(
   <Person age={'30'} profile={profile} />
)

Bear in mind that using shallow rendering won't provide any useful results when testing injected components; only the injector will be rendered. To test with shallow rendering, instantiate the wrappedComponent instead: shallow(<Person.wrappedComponent />)

disposeOnUnmount(componentInstance, propertyKey | function | function[])

Function (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.

import { disposeOnUnmount } from "mobx-react"

class SomeComponent extends React.Component {
    // decorator version
    @disposeOnUnmount
    someReactionDisposer = reaction(...)

    // decorator version with arrays
    @disposeOnUnmount
    someReactionDisposers = [
        reaction(...),
        reaction(...)
    ]


    // function version over properties
    someReactionDisposer = disposeOnUnmount(this, reaction(...))

    // function version inside methods
    componentDidMount() {
        // single function
        disposeOnUnmount(this, reaction(...))

        // or function array
        disposeOnUnmount(this, [
            reaction(...),
            reaction(...)
        ])
    }
}

DevTools

mobx-react@6 and higher are no longer compatible with the mobx-react-devtools. That is, the MobX react devtools will no longer show render timings or dependency trees of the component. The reason is that the standard React devtools are also capable of highlighting re-rendering components. And the dependency tree of a component can now be inspected by the standard devtools as well, as shown in the image below:

hooks.png

FAQ

Should I use observer for each component?

You should use observer on every component that displays observable data. Even the small ones. observer allows components to render independently from their parent and in general this means that the more you use observer, the better the performance become. The overhead of observer itself is negligible. See also Do child components need @observer?

I see React warnings about forceUpdate / setState from React

The following warning will appear if you trigger a re-rendering between instantiating and rendering a component:


Warning: forceUpdate(...): Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.`

-- or --


Warning: 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`.

Usually this means that (another) component is trying to modify observables used by this components in their constructor or getInitialState methods. This violates the React Lifecycle, componentWillMount should be used instead if state needs to be modified before mounting.

changelog

mobx-react

9.2.0

Minor Changes

Patch Changes

9.1.1

Patch Changes

9.1.0

Minor Changes

9.0.2

Patch Changes

9.0.1

Patch Changes

9.0.0

Major Changes

Patch Changes

8.0.0

Major Changes

  • 44a2cf42 #3590 Thanks @urugator! - Functional components now use useSyncExternalStore, which should prevent tearing - you have to update mobx, otherwise it should behave as previously.
    Improved displayName/name handling of functional components as discussed in #3438.
    Reactions of uncommited class components are now correctly disposed, fixes #3492.
    Reactions don't notify uncommited class components, avoiding the warning, fixes #3492.
    Removed symbol "polyfill" and replaced with actual Symbols.
    Removed this.render replacement detection + warning. this.render is no longer configurable/writable (possibly BC).
    Class component instance is no longer exposed as component[$mobx]["reactcomponent"] (possibly BC).
    Deprecated disposeOnUnmount, it's not compatible with remounting.

Patch Changes

7.6.0

Minor Changes

7.5.3

Patch Changes

7.5.2

Patch Changes

7.5.1

Patch Changes

  • bbcb12dc #3434 Thanks @urugator! - Support re-mounting of class components. Fixes #3395: observer not working with React@18 <StrictMode>.

7.5.0

Minor Changes

7.4.0

Minor Changes

  • 4c5e75cd #3382 Thanks @iChenLei! - replace the deprecated react type definition with recommended type definition

  • bd4b70d8 #3387 Thanks @mweststrate! - Added experimental / poor man's support for React 18. Fixes #3363, #2526. Supersedes #3005

    • Updated tests, test / build infra, peerDependencies to React 18
    • [breaking icmw upgrading to React 18] Already deprecated hooks like useMutableSource will trigger warnings in React 18, which is correct and those shouldn't be used anymore.
    • [breaking icmw upgrading to React 18] When using React 18, it is important that act is used in unit tests around every programmatic mutation. Without it, changes won't propagate!
    • The React 18 support is poor man's support; that is, we don't do anything yet to play nicely with Suspense features. Although e.g. startTransition basically works, MobX as is doesn't respect the Suspense model and will always reflect the latest state that is being rendered with, so tearing might occur. I think this is in theoretically addressable by using useSyncExternalStore and capturing the current values together with the dependency tree of every component instance. However that isn't included in this pull request 1) it would be a breaking change, whereas the current change is still compatible with React 16 and 17. 2) I want to collect use cases where the tearing leads to problems first to build a better problem understanding. If you run into the problem, please submit an issue describing your scenario, and a PR with a unit tests demonstrating the problem in simplified form. For further discussion see #2526, #3005

Patch Changes

7.3.0

Minor Changes

  • 59b42c28 #3282 Thanks @urugator! - observer(forwardRef(fn)) no longer generates extra <Observer> element and applies memo correctly

Patch Changes

7.2.1

Patch Changes

7.2.0

Minor Changes

  • 87b3e1de #2930 Thanks @vkrol! - inject shouldn't change original displayName of component that uses forwardRef

7.1.0

Patch Changes

7.0.6

Patch Changes

7.0.5

Patch Changes

7.0.4

Patch Changes

7.0.3

Patch Changes

7.0.2

Patch Changes

7.0.1

Patch Changes

  • 81a2f865 Thanks @FredyC! - ESM bundles without NODE_ENV present are available in dist folder. This useful for consumption in browser environment that supports ESM Choose either esm.production.min.js or esm.development.js from dist folder.

  • Updated dependencies [81a2f865]:

7.0.0

Release for compatibility with MobX v6

6.3.1

Patch Changes

6.3.0

  • Updated mobx-react-lite to 2.2.0 which removes the need to manually configure batching. Fixes #859

6.2.4

  • Fix error thrown in the already defined observer class component warning message when attempting to get the components display name. #887

6.2.3

  • Log warning if class component is already an observer to prevent memory leaks. #839
  • Fix disposeOnUnmount when using react-hot-loader. #725

6.2.2

  • Observer batching imports are kept in production builds as side effects (see issue)

6.2.1

  • Remove auto configured observer batching using react-dom. Fixes: #852.

6.2.0

  • Updated to latest mobx-react-lite V2 for compatibility with React.StrictMode.
  • Observer batching (see more in the docs).
  • Possibly breaking change, the dist/mobxreact.rn.module.js is no longer available, use dist/mobxreact.esm.js instead.

6.1.6 / 6.1.7

  • Fix an issue with class components & observableRequiresReaction. #806 through #829
  • Use TSDX for building to mitigate issues with accessing process.env #821

6.1.5

  • Added check if process.env is available, fixes #801 through #812 by @ynejati
  • Added warning if component's render method is accidentally overwritten. #799 by @Venryx. Helps prevent memory leaks as in: #797

6.1.4

6.1.2 / 6.1.3

  • Add reexport of useObserver from mobx-react-lite #734
  • Add the ability to pass multiple children to Provider
  • Fixed #717. Now inject works correctly with components that use React.forwardRef
  • Observer checks for use of React.memo #720
  • Get rid of the redundant Injector wrapper #716

6.1.1

  • Fixed issue where combining @disposeOnUnmount with disposeOnUnmount didn't clean up everything. Fixes #666 trough #671 by @JabX

6.1.0

  • Restored the classic implementation of observer: class based components are patched again, rather than wrapping them in <Observer>, see #703. Fixes:
    • componentDidUpdate not being triggered after a reactive render #692
    • The appearance of an additional <Observer> component in the component tree, which complicates shallow testing #699
    • Some regressions in disposeOnUnmount #702
    • Note that dev tool support, and other constraints mentioned in the 6.0.0 release notes have not been restored.
  • The function useStaticRendering(value: boolean): void from mobx-react-lite is now exposed

6.0.4

  • Fixed IE 11 compatibility which was accidentally broken. Fixes #698

6.0.3

  • disposeOnUnmount now supports initializing it with an array of disposers. Fixes #637 through #641 by @Amareis
  • Fixed hoisting of statically declared members. Fixes #678 through #682 by @meabed

6.0.2

  • Added missing types for MobXProviderContext, useLocalStore and useAsObservableSource. Fixes #679.

6.0.0

Breaking changes

  • The minimal supported version of React is 16.8.0
  • Killed the possibility to directly pass store names to observer. Always use inject instead. (This was deprecated for a long time already). observer(["a", "b"], component) should now be written as inject("a", "b")(component).
  • observer components no longer automatically recover from errors (to prevent potential memory leaks). Instead, this is the responsibility of error boundaries.
  • inject now supports ref forwarding. As such, the .wrappedInstance property has been removed since refs can be used instead. (Fixes #616 (See also #619 by 42shadow42)
  • Changing the set of stores in Provider is no longer supported and while throw a hard error (this was a warning before), as the model of Provider / inject has always been designed to inject final values into the tree. (That is, constanted references, the injected objects themselves can be stateful without problem). If you want to dynamically swap what is provided into the tree, use React.createContext instead of Provider / inject. The suppressChangedStoreWarningflag forProvider` has been dropped.
  • The third argument of custom storesToProps functions passed to inject is no longer available.
  • <Observer> no longer supports the deprecated inject property.
  • Defining shouldComponentUpdate on observer based components is no longer supported
  • propTypes is no longer exposed, use PropTypes instead
  • disposeOnUnmount now only supports direct subclasses of React.Component and React.PureComponent. This prevents several unreliable edge cases that silently leaked memory before. Either only extend React.(Pure)Component when using disposeOnUnmount, or manually clean up stuff in componentWillUnmount.
  • The onError global error handler has been removed. Use error boundaries instead.
  • Improved dev tool names for inject wrapped components, see #472 by SimeonC. Fixes #466
  • Dropped support for a build of mobx-react that doesn't target either react-dom or react-native. mobx-react doesn't need react-dom to be present, but to make sure your build tools don't fail, you might want to stub react-dom as an empty module.
  • The componentWillReact has been dropped
  • The MobX-react devtools (either as package or browser plugin) are no longer supported. Instead, the following tools can be analyzed to analyze your mobx-react application:
    • Visualizing re-rendering of components is now part of the standard React devtools
    • The dependency tree of a compent tree can be inspected by showing the state of the useObserver hook in the React devtools (at the time of this release it displays as just Object, but the next iteration of the React devtools will support those properly)
    • Spying on events can still be done with the MobX-react browser plugin, through the mobx-logger package or manually by using the spy or trace utility from the mobx package.

Improvements

  • Hook based components are now supported by mobx-react (in fact, the package is now implemented using hooks)
  • Class based observer components are now recommended to extend React.PureComponent. Functional observer components are now automatically wrapped in React.memo internally. See section in README for more details.
  • For observer based components, there will now be an additional Observer component in the tree.
  • Two new hooks have been exposed, in case you want to manage local state in observable: useLocalStore and useAsObservableSource.
  • MobXProviderContext is now exposed from the package, in case you want to consume the context used by Provider with a useContext hook.

5.4.3

  • Fixed #612, contextType was hoisted by inject, which shouldn't the case.

5.4.1 / 5.4.2

  • Fixed issue where react-is wasn't properly rolled-up into the package. Fixes #608

5.4.0

  • Added support for forward refs, fixes #602

5.3.6

  • Fixed some additional issues around life-cycle patching, take 3. See #536 by @xaviergonz. Fixed #579

5.3.5

5.3.4

  • Fixed unending recursing as a result of lifecylce patching. Fixes #579 through #582 by @xaviergonz

5.3.3

  • Fixed Cannot read property 'forEach' of undefined exception if disposeOnUnmount was called conditionally. #578 by Jef Hellemans

5.3.2

5.3.0 / 5.3.1

5.3.0 was retracted as files were not generated correctly during publish

  • Added disposeOnUnmount utility / decorator to call disposable properties (reaction, autorun, etc) automatically on componentWillUnmount
  • Introduced new method to patch lifecycle methods which should be more compatible with for example arrow functions.

5.2.8

  • Make sure mobx-react doesn't require Object.assign polyfill

5.2.7

  • Fixed issue where React 16.5 printed a warning when using Provider, fixes #545

5.2.6

  • Fixed bug in defining properties (although the bug had no known observable effect). Fixes #540

5.2.4 / 5.2.5

5.2.3

5.2.2

  • Polyfill Symbol if it doesn't exist. By @Strate through #499.

5.2.1

  • Component props and state properties are now made observable during the instance creation. This restores the behavior from before 5.1.0 where props and state could safely be observed during mount. Actually it is now possible to do similar things in constructors as well. Fixes #478. Thanks @Strate for the idea and PR! #496.

5.2.0

  • Added backward compatible support for MobX 5.
  • Fixed components sometimes being displayed as undefined in mobx-devtools. See #470 by @MauricioAndrades
  • Removed unnecessary warning @observer was used both on a sub and super class. See #492 by @skiritsis. N.B. putting @observer on a super and subclass is still not an supported pattern, use @observer on subclasses only!

5.1.2

  • Fixed regression bug in integration with devtools. Fixed through #465 by @le0nik

5.1.0

  • Added support for React 16.3, including support for the getDerivedStateFromProps life-cycle hook. MobX will no longer use componentWillMount hook internally, so that it can be used in StrictMode react as well. Fixes #447
  • Static properties of a function component are now automatically hoisted when the component is wrapped by observer. Implements #427
  • Misspelled export componentByNodeRegistery is now properly export as componentByNodeRegistry as well, please update consumers, the mispelled version will be dropped in the next major. Fixes #421
  • Deprecated the support for the inject property on Observer, it is fundamentally broken and should not be used. Use inject on the enclosing component instead and grab the necessary stores from the closure. Fixes #423
  • Added warning about using observer on a React.PureComponent, this will become an exception in the next major. Fixes #309
  • Mobx-react will now print a warning when combining observer with a custom shouldComponentUpdate implementation. Fixes #417

5.0.0

  • Added compatibility with MobX 4.x. This version is not compatible with older Mobx versions

4.4.3

  • The exposed React Native build now uses commonjs, to prevent the need of further transpilation. Fixes #428

4.4.2

  • Fixed issue with mobx-react not compiling on react-native due to the presence of a .babelrc file. Fixes #415 by Ryan Rampersad through #416

4.4.1

  • Fixed syntax error in 4.4.0 that escaped

4.4.0

  • Observer now supports render props, render and inject. See the updated readme. By ZiYingMai through #403
  • Fixed: NaN is now considered to be equal to NaN when doing reconciliation. Fixes #363, by Andrew Branch through #402
  • Improved typings of Observer component, by Rafał Filipek through #376
  • Fixed incorrect generation of component name, by Andy Kogut through #368
  • Lot of internal repo upgrades: Test suite is now in Jest, Prettier is used etc.

4.3.5

Fixed some issues with the typescript typings. See for example #353

4.3.4

Improved typescript typings, including support for strict mode in TS 2.6. Fixes

4.3.3

Added support for React 16. (No changes)

4.3.2

Killed accidentally exposed default exports.

If you are still using import mobxReact from "mobx-react", use import * as mobxReact from "mobx-react", or better import { whatYouNeed } from "mobx-react" instead.

4.3.1

4.3.0 (unpublished)

Improved module rollup setup, enabling better tree shaking. See #324 / #328

4.2.2

  • Fixed check for stateless components, by @leader22, see #280

4.2.1

Note: Due to pull / rebase issue the release commit is incorrect. This is the released commit

  • Reduced module size by 31% (switched to rollup.js). See #244 by @rossipedia
  • Skip creation of .wrappedInstance reference for stateless components. See #254 by @farwayer
  • Introduced global onError handler hook to be notified on errors thrown by @observer components. See #262 by @andykog
  • Improved typescript typings of the exposed propTypes, See #263 by @panjiesw

4.2.0

  • Same as 4.2.1, but contained build issue and is unpublished

4.1.8

  • Undid change introduced in 4.1.4 where the lifecycle hooks were protected, as this breaks react-hot-loader.... Fixes #231

4.1.7

  • Added support for React 15.5 (no deprecation warnings) and 16.0 (no proptypes / createClass), by @andykog, see #238. Fixes #233, #237

4.1.5

  • Improved typescript typings, fixes #223

4.1.4

  • Made lifecycle hooks used by mobx-react read-only to make sure they are not accidentally overwritten in component instances. Fixes, #195, #202. Note that they can still be defined, just make sure to define them on the prototype (componentWillMount() {}) instead of the instance (componentWillMount = () => {}). Which is best practice anyway.

4.1.3

  • Fixed ReactDOM.findDOMNode exception when using react-test-runner, #216

4.1.2

  • Exceptions caught during render are now rethrown with proper stack, fixes #206

4.1.1

  • Exposed wrappedInstance and wrappedComponent in typings
  • Fixed accidental use of default import from mobx package.

4.1.0

  • Added support for MobX3. Note that using MobX3 changes the error semantics. If an observer component throws, it will no longer crash the app, but just log the exceptions instead.

4.0.4

  • Introduced suppressChangedStoreWarning to optionally supresss change store warnings, by @dropfen, see #182, #183

4.0.3

  • Fixed issue where userland componentWilMount was run before observer componentWillMount

4.0.2

  • Fixed order of inject overloads, see #169
  • Fixed import of mobx when using Webpack without commonjs plugin, see: #168

4.0.1

  • Improved typings, by @timmolendijk, fixes #164, #166
  • Fixed inject signature in readme, by @farwayer

4.0.0

observer now uses shallow comparision for all props (Breaking change)

observer used to compare all properties shallow in the built-in shouldComponentUpdate, except when it received non-observable data structures. Because mobx-react cannot know whether a non observable has been deeply modified, it took no chances and just re-renders.

However, the downside of this when an unchanged, non-observable object is passed in to an observer component again, it would still cause a re-render. Objects such as styling etc. To fix this mobx-react will now always compare all properties in a pure manner. In general this should cause no trouble, as typically mutable data in mobx based objects is captured in observable objects, which will still cause components to re-render if needed.

If you need to pass in a deeply modified object and still want to make sure to cause a re-render, either

  • make sure the object / array is an observable
  • do not decorate your component with observer, but use Observer regions instead (see below)

See #160 for more details.

inject(fn)(component) will now track fn as well

inject(func) is now reactive as well, that means that transformations in the selector function will be tracked, see #111

const NameDisplayer = ({ name }) => <h1>{name}</h1>

const UserNameDisplayer = inject(stores => ({
    name: stores.userStore.name
}))(NameDisplayer)

const user = mobx.observable({
    name: "Noa"
})

const App = () => (
    <Provider userStore={user}>
        <UserNameDisplayer />
    </Provider>
)

ReactDOM.render(<App />, document.body)

N.B. note that in this specific case NameDisplayer doesn't have to be an observer, as it doesn't receive observables, but just plain data from the transformer function.

this.props and this.state in React components are now observables as well

A common cause of confusion were cases like:

@observer class MyComponent() {
    @computed upperCaseName() {
        return this.props.user.name.toUpperCase()
    }

    render() {
        return <h1>{this.upperCaseName}</h1>
    }
}

This component would re-render if user.name was modified, but it would still render the previous user's name if a complete new user was received! The reason for that is that in the above example the only observable tracked by the computed value is user.name, but not this.props.user. So a change to the first would be picked up, but a change in props itself, assigning a new user, not.

Although this is technically correct, it was a source of confusion. For that reason this.state and this.props are now automatically converted to observables in any observer based react component. For more details, see #136 by @Strate

Better support for Server Side Rendering

Introduced useStaticRendering(boolean) to better support server-side rendering scenarios. See #140

Introduced Observer as alternative syntax to the observer decorator.

This feature is still experimental and might change in the next minor release, or be deprecated

Introduced Observer. Can be used as alternative to the observer decorator. Marks a component region as reactive. See the Readme / #138 Example:

const UserNameDisplayer = ({ user }) => <Observer>{() => <div>{user.name}</div>}</Observer>

Using observer to inject stores is deprecated

The fact that observer could inject stores as well caused quite some confusion. Because in some cases observer would return the original component (when not inject), but it would return a HoC when injecting. To make this more consistent, you should always use inject to inject stores into a component. So use:

@inject("store1", "store2") @observer
class MyComponent extends React.Component {

or:

const MyComponent = inject("store1", "store2")(observer(props => rendering))

For more info see the related discussion

Other improvements

  • If mobx and mobx-react are used in combination, all reactions are run as part of React's batched updates. This minimizes the work of the reconciler, guarantees optimal rendering order of components (if the rendering was not triggered from within a React event). Tnx @gkaemmer for the suggestion.
  • It is now possible to directly define propTypes and defaultProps on components wrapped with inject (or observer(["stores"])) again, see #120, #142. Removed the warnings for this, and instead improved the docs.
  • Clean up data subscriptions if an error is thrown by an observer component, see #134 by @andykog
  • export PropTypes as well in typescript typings, fixes #153
  • Add react as a peer dependency
  • Added minified browser build: index.min.js, fixes #147
  • Generate better component names when using inject

3.5.9

  • Print warning when inject and observer are used in the wrong order, see #146, by @delaetthomas

3.5.8

  • Fixed issue where props where not passed properly to components in very rare cases. Also fixed #115

3.5.7

  • Bundles are no longer minified, fixes #127

3.5.6

  • Export propTypes as PropTypes, like React (@andykog, ##117)

3.5.5

  • Removed experimental status of inject / Provider. Official feature now.
  • Fixed hot-reloading issue, #101

3.5.4

  • Introduced wrappedInstance by @rossipedia on inject decorated HOC's, see https://github.com/mobxjs/mobx-react/pull/90/
  • print warnings when assign values to propTypes, defaultProps, or contextTypes of a HOC. (by @jtraub, see https://github.com/mobxjs/mobx-react/pull/88/)
  • Static properties are now hoisted to HoC components when, #92
  • If inject is used incombination with a function, the object return from the function will now be merged into the nextProps instead of replacing them, #80
  • Always do propType checking untracked, partially fixes #56, #305

3.5.3

  • Fixed error Cannot read property 'renderReporter' of undefined (#96)

3.5.2

  • Added propTypes.observableArrayOf and propTypes.arrayOrObservableArrayOf (#91)

3.5.1

  • Fixed regression #85, changes caused by the constructor results in inconsistent rendering (N.B.: that is un-idiomatic React usage and React will warn about this!)

3.5.0

  • Introduced inject("store1", "store2")(component) as alternative syntax to inject stores. Should address #77, #70
  • Introduced the wrappedComponent property on injected higher order components, addresses #70, #72
  • Fixed #76: error when no stores are provided through context
  • Added typings for devTools related features (@benjamingr).
  • Added MobX specific propTypes (@mattruby)
  • Merged #44, fixes #73: don't re-render if component was somehow unmounted

3.4.0

  • Introduced Provider / context support (#53 / MobX #300)
  • Fixed issues when using devtools with IE. #66 (By @pvasek)

3.3.1

  • Added typescript typings form mobx-react/native and mobx-react/custom
  • Fixed #63: error when using stateless function components when using babel and typescript

3.3.0

  • Upgraded to MobX 2.2.0

3.2.0

  • Added support for react-native 0.25 and higher. By @danieldunderfelt.

3.1.0

  • Added support for custom renderers (without DOM), use: mobx-react/custom as import fixes #42
  • Fixed some issues with rollup #43
  • Minor optimization

3.0.5

Introduced componentWillReact

3.0.4

The debug name stateless function components of babel transpiled jsx are now properly picked up if the wrapper is applied after defining the component:

const MyComponent = () => <span>hi</span>

export default observer(MyComponent)

3.0.3

Removed peer dependencies, React 15 (and 0.13) are supported as well. By @bkniffler

3.0.2

Removed the warning introduced in 3.0.1. It triggered always when using shallow rendering (when using shallow rendering componentDidMount won't fire. See https://github.com/facebook/react/issues/4919).

3.0.1

Added warning when changing state in getInitialState / constructor.

3.0.0

Upgraded to MobX 2.0.0

2.1.5

Improved typescript typings overloads of observer

2.1.4

Added empty 'dependencies' section to package.json, fixes #26

2.1.3

Added support for context to stateless components. (by Kosta-Github).

2.1.1

Fixed #12: fixed React warning when a component was unmounted after scheduling a re-render but before executing it.

2.1.0

Upped dependency of mobx to 1.1.1.

2.0.1

It is now possible to define propTypes and getDefaultProps on a stateless component:

const myComponent = props => {
    // render
}

myComponent.propTypes = {
    name: React.PropTypes.string
}

myComponent.defaultProps = {
    name: "World"
}

export default observer(myComponent)

All credits to Jiri Spac for this contribution!

2.0.0

Use React 0.14 instead of React 0.13. For React 0.13, use version `mobx-react@1.0.2` or higher.

1.0.2

Minor fixes and improvements

1.0.1

Fixed issue with typescript typings. An example project with MobX, React, Typescript, TSX can be found here: https://github.com/mobxjs/mobx-react-typescript

1.0.0

reactiveComponent has been renamed to observer

0.2.3

Added separte import for react-native: use var reactiveComponent = require('mobx-react/native').reactiveComponent for native support; webpack clients will refuse to build otherwise.

0.2.2

Added react-native as dependency, so that the package works with either react or react-native.

0.2.0

Upgraded to MobX 0.7.0

0.1.7

Fixed issue where Babel generated component classes where not properly picked up.

0.1.6

observer now accepts a pure render function as argument, besides constructor function. For example:

var TodoItem = observer(function TodoItem(props) {
    var todo = props.todo
    return <li>{todo.task}</li>
})

0.1.5

observer is now defined in terms of side effects.

0.1.4

Added support for React 0.14(RC) by dropping peer dependency