Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mobxjs/mobx-react
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5.3.6
Choose a base ref
...
head repository: mobxjs/mobx-react
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5.4.0
Choose a head ref
  • 12 commits
  • 6 files changed
  • 6 contributors

Commits on Oct 30, 2018

  1. fix: typo

    fi3ework committed Oct 30, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    80a0644 View commit details
  2. Merge pull request #592 from fi3ework/master

    fix: typo
    mattruby authored Oct 30, 2018
    Copy the full SHA
    2e2cf22 View commit details

Commits on Nov 13, 2018

  1. Copy the full SHA
    799deb8 View commit details

Commits on Nov 14, 2018

  1. 1
    Copy the full SHA
    cc94b69 View commit details

Commits on Nov 16, 2018

  1. Merge pull request #604 from noscripter/jest-jsdom

    test: add testURL to fix jsdom SecurityError
    mweststrate authored Nov 16, 2018
    Copy the full SHA
    1bb4fa9 View commit details

Commits on Nov 17, 2018

  1. Copy the full SHA
    be37147 View commit details

Commits on Nov 19, 2018

  1. Copy the full SHA
    79aa7b8 View commit details
  2. Copy the full SHA
    66afcf7 View commit details
  3. Copy the full SHA
    fcf7a21 View commit details
  4. Copy the full SHA
    7d49342 View commit details
  5. Copy the full SHA
    1db7fe7 View commit details
  6. Published version 5.4.0

    mweststrate committed Nov 19, 2018
    Copy the full SHA
    ea11d06 View commit details
Showing with 87 additions and 3 deletions.
  1. +5 −0 CHANGELOG.md
  2. +2 −0 README.md
  3. +5 −2 package.json
  4. +20 −1 src/observer.js
  5. +23 −0 test/__snapshots__/stateless.test.js.snap
  6. +32 −0 test/stateless.test.js
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# MobX-React Changelog

### 5.4.0

* Added support for forward refs, fixes [#602](https://github.com/mobxjs/mobx-react/issues/602)

### 5.3.6

* Fixed some additional issues around life-cycle patching, take 3. See [#536](https://github.com/mobxjs/mobx-react/pull/586) by [@xaviergonz](https://github.com/xaviergonz). Fixed [#579](https://github.com/mobxjs/mobx-react/issues/579)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ import { observer } from "mobx-react/custom"
This package provides the bindings for MobX and React.
See the [official documentation](http://mobxjs.github.io/mobx/intro/overview.html) for how to get started.

If 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).

## Boilerplate projects that use mobx-react

* Minimal MobX, React, ES6, JSX, Hot reloading: [MobX-React-Boilerplate](https://github.com/mobxjs/mobx-react-boilerplate)
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-react",
"version": "5.3.6",
"version": "5.4.0",
"description": "React bindings for MobX. Create fully reactive components.",
"main": "index.js",
"jsnext:main": "index.module.js",
@@ -73,6 +73,7 @@
},
"dependencies": {
"hoist-non-react-statics": "^3.0.0",
"react-is": "^16.3.2",
"react-lifecycles-compat": "^3.0.2"
},
"keywords": [
@@ -89,7 +90,9 @@
"git add"
]
},
"jest": {},
"jest": {
"testURL": "http://127.0.0.1/"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
21 changes: 20 additions & 1 deletion src/observer.js
Original file line number Diff line number Diff line change
@@ -2,10 +2,13 @@ import React, { Component, PureComponent } from "react"
import hoistStatics from "hoist-non-react-statics"
import { createAtom, Reaction, _allowStateChanges, $mobx } from "mobx"
import { findDOMNode as baseFindDOMNode } from "react-dom"

import EventEmitter from "./utils/EventEmitter"
import inject from "./inject"
import { patch as newPatch, newSymbol } from "./utils/utils"

const ReactIs = require("react-is")

const mobxAdminProperty = $mobx || "$mobx"
const mobxIsUnmounted = newSymbol("isUnmounted")

@@ -183,7 +186,7 @@ function makeComponentReactive(render) {
if (this[mobxIsUnmounted] !== true) {
// If we are unmounted at this point, componentWillReact() had a side effect causing the component to unmounted
// TODO: remove this check? Then react will properly warn about the fact that this should not happen? See #73
// However, people also claim this migth happen during unit tests..
// However, people also claim this might happen during unit tests..
let hasError = true
try {
setHiddenProp(this, isForcingUpdateKey, true)
@@ -318,6 +321,22 @@ export function observer(arg1, arg2) {
)
}

// Unwrap forward refs into `<Observer>` component
// we need to unwrap the render, because it is the inner render that needs to be tracked,
// not the ForwardRef HoC
if (componentClass["$$typeof"] === ReactIs.ForwardRef) {
const baseRender = componentClass.render
if (typeof baseRender !== "function")
throw new Error("render property of ForwardRef was not a function")
return {
...componentClass,
render() {
const args = arguments
return <Observer>{() => baseRender.apply(undefined, arguments)}</Observer>
}
}
}

// Stateless function component:
// If it is function but doesn't seem to be a react class constructor,
// wrap it to a react class automatically
23 changes: 23 additions & 0 deletions test/__snapshots__/stateless.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stateless component with forwardRef is reactive 1`] = `
<div>
result:
hello world
,
got ref
, a.x:
2
</div>
`;

exports[`stateless component with forwardRef render test correct 1`] = `
<div>
result:
hello world
,
got ref
, a.x:
1
</div>
`;
32 changes: 32 additions & 0 deletions test/stateless.test.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ import TestUtils from "react-dom/test-utils"
import * as mobx from "mobx"
import { observer, propTypes } from "../src"
import { createTestRoot } from "./index"
import renderer from "react-test-renderer"
import { observable } from "mobx"

const testRoot = createTestRoot()

@@ -74,3 +76,33 @@ test("component with observable propTypes", () => {
expect(warnings.length).toBe(1)
console.error = originalConsoleError
})

describe("stateless component with forwardRef", () => {
const a = observable({
x: 1
})
const ForwardRefCompObserver = observer(
React.forwardRef(({ testProp }, ref) => {
return (
<div>
result: {testProp}, {ref ? "got ref" : "no ref"}, a.x: {a.x}
</div>
)
})
)

test("render test correct", () => {
const component = renderer.create(
<ForwardRefCompObserver testProp="hello world" ref={React.createRef()} />
)
expect(component).toMatchSnapshot()
})

test("is reactive", () => {
const component = renderer.create(
<ForwardRefCompObserver testProp="hello world" ref={React.createRef()} />
)
a.x++
expect(component).toMatchSnapshot()
})
})