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

Make props & state observable in constructor & componentWillMount #496

Merged

Conversation

Strate
Copy link
Contributor

@Strate Strate commented Jun 7, 2018

Moved instrumentation of state & props to observer decorator.
Closes #478

Moved instrumentation of state & props to `observer` decorator.
Closes mobxjs#478
src/observer.js Outdated
@@ -177,17 +157,12 @@ function makeComponentReactive(render) {
* If props are shallowly modified, react will render anyway,
* so atom.reportChanged() should not result in yet another re-render
*/
let skipRender = false
this[skipRenderKey] = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this a non-enumerable prop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes, of course

src/observer.js Outdated
makePropertyObservableReference.call(this, "props")
// make state an observable reference
makePropertyObservableReference.call(this, "state")
this[isForcingUpdateKey] = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clarify, dont understood :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, making this a non-enumerable prop as well :)

src/observer.js Outdated
const valueHolderKey = Symbol(propName + " value holder")
const atomHolderKey = Symbol(propName + " atom holder")
function getAtom() {
return this[atomHolderKey] || (this[atomHolderKey] = createAtom("reactive " + propName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this a non-enumerable prop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Will address this a bit later today.

@@ -273,6 +248,32 @@ const reactiveMixin = {
}
}

function makeObservableProp(target, propName) {
const valueHolderKey = Symbol(propName + " value holder")
const atomHolderKey = Symbol(propName + " atom holder")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether using Symbols is a breaking change. So far we didn't depend on them, but I think React itself already requires Symbol or a polyfill of it? (Since mobx-react 5 can still be used with mobx 4 so should work on any ES 5 env)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check this out, maybe there is a way to do this without symbols.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The support looks pretty good to me: https://caniuse.com/#search=symbol

Copy link
Member

@mweststrate mweststrate Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without symbols it could be done by putting an object under some ugly key. Symbols is preferred though :) You could always create a super naive polyfill like: function Symbol(name) { return ``$mobxReactProp${symbolname}${Math.random()}`` }. But since react itself already requires quite some polyfills (link, so I think we can bet on Symbol to be present. And otherwise fix it if somebody reports it

@mweststrate
Copy link
Member

Looks like a great improvement! Left some questions

@Strate
Copy link
Contributor Author

Strate commented Jun 7, 2018

Made internal props hidden, but keep Symbol as is.

@mweststrate mweststrate merged commit 25f8389 into mobxjs:master Jun 8, 2018
@mweststrate
Copy link
Member

Merging! Will release soon :-)

@Strate
Copy link
Contributor Author

Strate commented Jun 8, 2018

Wow, cool! Can't wait for release :)

@mweststrate
Copy link
Member

Released as 5.2.1!

@Strate Strate mentioned this pull request Jun 8, 2018
@TomaszGrzelak
Copy link

@mweststrate - Sadly I've just run into a problem that is 90% likely to be connected with the changes discussed here (got 5.2.1 installed just a few hours ago).
What I found, is that Symbol is not natively supported on Android (using React-Native).
I managed to get things working by adding import 'es6-symbol/implement'; at the very beginning of my index.js file.

@Strate
Copy link
Contributor Author

Strate commented Jun 8, 2018

@TomaszGrzelak oh, sorry for that. I gonna to make PR to naive polyfill Symbol

@Strate
Copy link
Contributor Author

Strate commented Jun 8, 2018

@TomaszGrzelak #499

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants