Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype React 16 context #19

Closed
wants to merge 4 commits into from
Closed

Prototype React 16 context #19

wants to merge 4 commits into from

Conversation

ghmeier
Copy link
Contributor

@ghmeier ghmeier commented May 16, 2018

Hey, wanted to start the conversation on this since the current version of connect-backbone-to-react does not work when used with React 16.3's new context apis like getSnapshotBeforeUpdate. This PR mimics existing functionality using the new Provider and Consumer components while removing soon-to-be-deprecated methods like componentWillReceiveProps as well as the old context lifecycle methods.

Note that the tests here fail since the current version of enzyme doesn't support the new context methods (the related issue is here: enzymejs/enzyme#1553).

Spencer Brown and others added 3 commits June 19, 2017 14:22
In [React
16.3](https://reactjs.org/blog/2018/03/29/react-v-16-3.html#official-context-api)
they announced a new, official Context API. Here we can use this api to
simplify the `BackboneProvider` and the `connectBackboneToReact`
function. We no longer need to use soon to be deprecated lifecycle
methods and can instead just set up listeners for backbone events and
map models to props as previously.

Fortunately, we can accomplish this without changes to the
`connect-backbone-to-react` API, but unfortunately, it requires updating
to `react@^16.3.0` (along with `prop-types` and `react-dom`). This also
means that tests have to be updated to use the latest version of
`enzyme` which doesn't [yet
support](enzymejs/enzyme#1553) the new Context
API. Until then, we can leave this as a prototype.
Copy link
Member

@hswolff hswolff left a comment

Choose a reason for hiding this comment

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

Thank you for taking this on!

Definitely interested in adopting the new features of React 16 into this library so very excited to see this.

I'm seeing some existing eslint errors in the Travis build. Hoping if we get that fixed we can see if the tests can run at all.

I also have some initial feed back on this PR.

I think we'd release this as a new major version as it contains some breaking changes from how it works underneath the hood.

Again, thank you!

const PropTypes = require('prop-types');
const ConnectBackboneToReactContext = require('./context.js'); // eslint-disable-line no-unused-vars
Copy link
Member

Choose a reason for hiding this comment

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

nit, remove file extension.

Also is this eslint still needed?

@@ -0,0 +1,2 @@
const React = require('react');
module.exports = React.createContext('connectBackboneToReact');
Copy link
Member

Choose a reason for hiding this comment

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

The argument to createContext is the default value. Our default value is not a string. We can safely omit that.

const PropTypes = require('prop-types');
const debounceFn = require('lodash.debounce');
const ConnectBackboneToReactContext = require('./context.js'); // eslint-disable-line no-unused-vars
Copy link
Member

Choose a reason for hiding this comment

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

ditto comment in other file

}

setModels(props, context) {
const models = Object.assign({}, context.models, props.models);
setModels(models = {}) {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we don't use this method anymore.

}

setModels(props, context) {
const models = Object.assign({}, context.models, props.models);
setModels(models = {}) {
validateModelTypes(models);
this.models = models;
Copy link
Member

Choose a reason for hiding this comment

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

I don't see this being updated ever either. I'm curious if we even need it anymore.

});
validateModelTypes(this.models);
Copy link
Member

Choose a reason for hiding this comment

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

We should be validating models before we create event listeners. If we don't have a test for this already we should add one.

super(props);

this.state = {
models: props.models,
};
}

Copy link
Member

Choose a reason for hiding this comment

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

We need to implement getDerivedStateFromProps to copy over new prop models when they are given.

Or I wonder if we could just use this.props as the value given to the provider.

This fixes the tests for CBR when it uses react context.
@hswolff
Copy link
Member

hswolff commented Mar 9, 2020

We managed to merge this in via #25.

@hswolff hswolff closed this Mar 9, 2020
@leamarty leamarty deleted the react-16-context branch May 12, 2022 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants