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

mobx-react v6 #644

Merged
merged 49 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
998b2e6
fixes HOC displayName for observer and inject
May 7, 2018
6f8138c
Started V6
mweststrate Feb 11, 2019
b266442
Upgraded React to 16.8.0
mweststrate Feb 11, 2019
ac24719
Killed the `observer` overload that accepts store names` for auto inject
mweststrate Feb 11, 2019
61ae046
Make sure reactions / components stof if a component throws exception…
mweststrate Feb 11, 2019
886cb96
Fixed broken inject test
mweststrate Feb 11, 2019
bf54f75
Implemented forwardRefs. Fixes #616 / #619
mweststrate Feb 11, 2019
825056c
Better links in changelog
mweststrate Feb 11, 2019
cd9977e
Killed life-cycle compat
mweststrate Feb 11, 2019
5cd7b53
Basic implementation for modern context
mweststrate Feb 11, 2019
840a2f1
moved `shallowEqual` and `is` utilities
mweststrate Feb 11, 2019
a64c2d3
`Provider`: Forbid changing stores
mweststrate Feb 11, 2019
6957468
Some provider / inject cleanup
mweststrate Feb 11, 2019
b015cd3
Removed duplicate context/inject tests
mweststrate Feb 11, 2019
bc098c6
Fixed warning generated by inject
mweststrate Feb 11, 2019
7e32fc0
Provider can now redefine / combine stores
mweststrate Feb 11, 2019
3327e0f
Some random cleanup
mweststrate Feb 11, 2019
c8212aa
no longer supports property
mweststrate Feb 13, 2019
2ea0f15
WIP: use observer from mobx-react-lite for function components
mweststrate Feb 14, 2019
f68bf60
Fixed most remaining tests
mweststrate Feb 14, 2019
0c77fd6
Use `Observer` from mobx-react-lite
mweststrate Feb 14, 2019
e8e61b5
modernized rollup
mweststrate Feb 14, 2019
6807429
Blegh, filesize keeps failing
mweststrate Feb 14, 2019
f34dab3
Jest update
mweststrate Feb 14, 2019
1ea40e2
use `observer` implementation from mobx-react-lite
mweststrate Feb 14, 2019
bb9b55e
Got rid of enzyme
mweststrate Feb 15, 2019
44a9b93
Fixed typescript tests
mweststrate Feb 15, 2019
23f0d05
Use microbundle for builds
mweststrate Feb 15, 2019
58d785d
Some cleanup
mweststrate Feb 17, 2019
d282c22
Some failing tests for disposeOnUnmount
mweststrate Feb 17, 2019
185150b
Fixed test in stateless
mweststrate Feb 17, 2019
49b0bd5
Killed avanced patching; directly extend React.(Pure)Component or dis…
mweststrate Feb 17, 2019
50f03a7
Removed / updated tests where relevant
mweststrate Feb 17, 2019
79a5dc1
Updating Readme / processing more todo's
mweststrate Feb 17, 2019
c85257f
Merge branch 'v6-radical' into v6
mweststrate Feb 17, 2019
2f6c76e
Apply suggestions from code review
FredyC Feb 19, 2019
edc676b
Merge branch 'hoc-conventions' of /~https://github.com/SimeonC/mobx-rea…
mweststrate Mar 10, 2019
9554f64
Merge branch 'SimeonC-hoc-conventions' into v6
mweststrate Mar 10, 2019
96c9102
Fixed changelog
mweststrate Mar 10, 2019
5116529
Removed hoist-statics dependency
mweststrate Mar 21, 2019
464035b
Fixed several typescript issues
mweststrate Mar 21, 2019
53f1dd2
Generate react-native build during compilation, so that there are no …
mweststrate Mar 21, 2019
6d23b11
Disabled compressing since it seems to break React
mweststrate Mar 22, 2019
24a5b35
Proper fix
mweststrate Mar 22, 2019
d152895
upgraded mobx-react-lite, processed some todo's
mweststrate May 29, 2019
d0a3661
don't bundle mobx-react-lite, but depend on it
mweststrate May 29, 2019
ce77e7e
Added some tests for hooks
mweststrate May 29, 2019
516a21e
Documentation improvements
mweststrate May 29, 2019
d5c832f
Some changelog / readme improvements
mweststrate May 29, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* `inject` now supports ref forwarding. As such, the `.wrappedInstance` property has been removed since refs can be used instead. (Fixes [#616](/~https://github.com/mobxjs/mobx-react/issues/616) (See also [#619](/~https://github.com/mobxjs/mobx-react/pull/619) by [42shadow42](/~https://github.com/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 to inject final values into the tree. (That is, fixed references, 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 suppressChangedStoreWarning` flag for `Provider` 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.

**Improvements**

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"typescript": "^2.6.0"
},
"dependencies": {
"hoist-non-react-statics": "^3.0.0"
"hoist-non-react-statics": "^3.0.0",
"mobx-react-lite": "^1.0.0"
},
"keywords": [
"mobx",
Expand All @@ -96,4 +97,4 @@
"pre-commit": "lint-staged"
}
}
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ if (!spy) throw new Error("mobx-react requires mobx to be available")
if (typeof rdBatched === "function") configure({ reactionScheduler: rdBatched })
else if (typeof rnBatched === "function") configure({ reactionScheduler: rnBatched })

// TODO: re-export mobx-react-lite stuff?
danielkcz marked this conversation as resolved.
Show resolved Hide resolved

export {
observer,
Observer,
Expand Down
3 changes: 0 additions & 3 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ function createStoreInjector(grabStoresFn, component, injectNames, makeReactive)
static contextType = MobXProviderContext

render() {
// Optimization: it might be more efficient to apply the mapper function *outside* the render method
// (if the mapper is a function), that could avMobXProviderContextoid expensive(?) re-rendering of the injector component
// See this test: 'using a custom injector is not too reactive' in inject.js
const { forwardRef, ...props } = this.props

Object.assign(props, grabStoresFn(this.context || {}, props) || {})
Expand Down
37 changes: 13 additions & 24 deletions src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import React, { Component, PureComponent, forwardRef } from "react"
import hoistStatics from "hoist-non-react-statics"
import { createAtom, Reaction, _allowStateChanges, $mobx } from "mobx"
import { findDOMNode as baseFindDOMNode } from "react-dom"
import {
observer as observerLite,
useStaticRendering as useStaticRenderingLite
} from "mobx-react-lite"

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

const mobxAdminProperty = $mobx || "$mobx"
Expand All @@ -17,8 +20,6 @@ let isDevtoolsEnabled = false

let isUsingStaticRendering = false

let warnedAboutObserverInjectDeprecation = false

// WeakMap<Node, Object>;
export const componentByNodeRegistry = typeof WeakMap !== "undefined" ? new WeakMap() : undefined
export const renderReporter = new EventEmitter()
Expand Down Expand Up @@ -84,6 +85,7 @@ export function trackComponents() {

export function useStaticRendering(useStaticRendering) {
isUsingStaticRendering = useStaticRendering
useStaticRenderingLite(useStaticRendering)
}

/**
Expand Down Expand Up @@ -299,18 +301,12 @@ export function observer(componentClass) {
!componentClass.isReactClass &&
!Component.isPrototypeOf(componentClass)
) {
const observerComponent = observer(
danielkcz marked this conversation as resolved.
Show resolved Hide resolved
class extends Component {
static displayName = componentClass.displayName || componentClass.name
static contextTypes = componentClass.contextTypes
static propTypes = componentClass.propTypes
static defaultProps = componentClass.defaultProps
render() {
return componentClass.call(this, this.props, this.context)
}
}
)
const observerComponent = observerLite(componentClass)
// TODO: move to mobx-react-lite
mweststrate marked this conversation as resolved.
Show resolved Hide resolved
hoistStatics(observerComponent, componentClass)
if (componentClass.propTypes) observerComponent.propTypes = componentClass.propTypes
if (componentClass.defaultProps)
observerComponent.defaultProps = componentClass.defaultProps
return observerComponent
}

Expand Down Expand Up @@ -348,20 +344,13 @@ function mixinLifecycleEvents(target) {
}
}

export const Observer = observer(({ children, inject: observerInject, render }) => {
// TODO: use mobx-react-lite version?
mweststrate marked this conversation as resolved.
Show resolved Hide resolved
export const Observer = observer(({ children, render }) => {
const component = children || render
if (typeof component === "undefined") {
return null
}
if (!observerInject) {
return component()
}
// TODO: remove in next major
console.warn(
"<Observer inject=.../> is no longer supported. Please use inject on the enclosing component instead"
)
const InjectComponent = inject(observerInject)(component)
return <InjectComponent />
return component()
})

Observer.displayName = "Observer"
Expand Down
32 changes: 10 additions & 22 deletions test/__snapshots__/observer.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`use Observer inject and render sugar should work use children with inject should be correct 1`] = `
Object {
"errors": Array [],
"infos": Array [],
"warnings": Array [
Array [
"<Observer inject=.../> is no longer supported. Please use inject on the enclosing component instead",
],
],
}
`;

exports[`use Observer inject and render sugar should work use render with inject should be correct 1`] = `
Object {
"errors": Array [],
"infos": Array [],
"warnings": Array [
Array [
"<Observer inject=.../> is no longer supported. Please use inject on the enclosing component instead",
],
],
}
exports[`should stop updating if error was thrown in render (#134) 1`] = `
Array [
"Error: Hello",
Object {
"componentStack": "
in X
in div (created by Outer)
in Outer",
},
]
`;
39 changes: 1 addition & 38 deletions test/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,43 +305,6 @@ describe("inject based context", () => {
expect(ref.current.testField).toBe(1)
})

test.skip("warning is printed when attaching contextTypes to HOC", () => {
// TODO: can be removed once using modern context?
const msg = []
const baseWarn = console.warn
console.warn = m => msg.push(m)
const C = inject("foo")(
createClass({
displayName: "C",
render() {
return (
<div>
context:
{this.props.foo}
</div>
)
}
})
)
C.propTypes = {}
C.defaultProps = {}
C.contextTypes = {}

const B = () => <C />
const A = () => (
<Provider foo="bar">
<B />
</Provider>
)
mount(<A />)
expect(msg.length).toBe(1)
expect(msg[0]).toBe(
"Mobx Injector: you are trying to attach `contextTypes` on an component decorated with `inject` (or `observer`) HOC. Please specify the contextTypes on the wrapped component instead. It is accessible through the `wrappedComponent`"
)

console.warn = baseWarn
})

test("propTypes and defaultProps are forwarded", () => {
const msg = []
const baseError = console.error
Expand Down Expand Up @@ -427,7 +390,7 @@ describe("inject based context", () => {
console.warn = baseWarn
})

test("using a custom injector is reactive", async () => {
test("using a custom injector is reactive", () => {
const user = mobx.observable({ name: "Noa" })
const mapper = stores => ({ name: stores.user.name })
const DisplayName = props => <h1>{props.name}</h1>
Expand Down
31 changes: 24 additions & 7 deletions test/misc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ReactDOM from "react-dom"
import { mount, shallow } from "enzyme"
import * as mobx from "mobx"
import { observer } from "../src"
import { createTestRoot, withConsole } from "./index"
import { createTestRoot, withConsole, asyncReactDOMRender } from "./index"

const mobxAdminProperty = mobx.$mobx || "$mobx"

Expand All @@ -18,7 +18,12 @@ describe("custom shouldComponentUpdate is not respected for observable changes (
const x = mobx.observable.box(3)
const C = observer(
createClass({
render: () => <div>value:{x.get()}</div>,
render: () => (
<div>
value:
{x.get()}
</div>
),
shouldComponentUpdate: () => called++
})
)
Expand All @@ -45,7 +50,12 @@ describe("custom shouldComponentUpdate is not respected for observable changes (
const C = observer(
createClass({
render() {
return <div>value:{this.props.y}</div>
return (
<div>
value:
{this.props.y}
</div>
)
},
shouldComponentUpdate(nextProps) {
called++
Expand Down Expand Up @@ -131,13 +141,20 @@ test("#85 Should handle state changing in constructors", done => {
a.set(3) // one shouldn't do this!
return {}
},
render: () => <div>child:{a.get()} - </div>
render: () => (
<div>
child:
{a.get()} -{" "}
</div>
)
})
)
const ParentWrapper = observer(function Parent() {
return (
<span>
<Child />parent:{a.get()}
<Child />
parent:
{a.get()}
</span>
)
})
Expand Down Expand Up @@ -174,9 +191,9 @@ test("testIsComponentReactive", () => {
})

test("testGetDNode", () => {
const C = observer(() => null)
const C = observer(() => <div />)

const wrapper = mount(<C />)
const wrapper = renderer.create(<C />)
expect(wrapper.instance().render[mobxAdminProperty]).toBeTruthy()
expect(mobx.getAtom(wrapper.instance().render)).toBeTruthy()

Expand Down
Loading