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

New React 16.3 context API #153

Merged
merged 9 commits into from
Apr 26, 2018
Merged

New React 16.3 context API #153

merged 9 commits into from
Apr 26, 2018

Conversation

elboman
Copy link
Member

@elboman elboman commented Apr 15, 2018

This PR is to track the work needed to support and use the new context api introduced in React 16.3

To get the initial work done I've monkey-patched React type definitions and added a custom react-16-adapter to get typescript and enzyme to work correctly, but I haven't committed any of this.

Things needed are for the release are:

  • Update components to use Providers/Consumers API
  • Update old tests to reflect the changes
  • Add new tests for UIRouterConsumer and possibly new edge cases
  • Wait for @types to officially support React 16.3 (issue)
  • Wait for enzyme to officially support React 16.3 (issue)

Once everything is addressed we can merge and release 0.7.0 🎉

@elboman elboman changed the title Context api New React 16.3 context API Apr 15, 2018
elboman added 9 commits April 26, 2018 12:35
Add `tsconfig-path-webpack-plugin` in typescript example in order to use the `tsconfig.json` `paths` options as webpack resolves. This lets us use the local dev version of `@uirouter/react` in the example for debugging purposes.
feat(UIRouterConsumer): add new `<UIRouterConsumer>` component to access the router instance via the new context API.

example:
```jsx
import {UIRouterConsumer} from '@uirouter/react';

class SomeComponent extends React.Component {
  render () {
    const router = this.props.router;
    // use the router via props
    return <div>whatever</div>
  }
}

export default () =>
  <UIRouterConsumer>
    {router => <SomeComponent router={router} />}
  </UIRouterConsumer>
```

BREAKING CHANGE: `@uirouter/react` now uses the new React 16.3 Context API. If you were accessing the router instance via the legacy context api (which was never explecitly supported) you need to update your code accordingly:

before:
```jsx
class SomeComponent extends React.Component {
  static contextTypes = {
    router: PropTypes.object
  }

  render () {
    // access context via this.context
    const router = this.context.router;
    // do whatever needed with the router
  }
}
```

after:
```jsx
class SomeComponent extends React.Component {
  render () {
    // access router via props
    const router = this.props.router;
    // do whatever needed with the router
  }
}

// when rendering the component wrap it with the `<UIRouterConsumer>` component
<UIRouterConsumer>
  {router => <SomeComponent router={router} />}
</UIRouterConsumer>
```

Closes #54
BREAKING CHANGE: from version `0.7.0` `@uirouter/react` only supports react from version `16.3.x` because of the new Context API.
If you need to use it with previous versions of React you should check the `0.6.x`, but bear in mind that it’s no longer supported and it’s advised to update React instead.
Enzyme does not support React 16.3 context API.
Patch the package until the package is realsed with the fix.
enzymejs/enzyme#1513
@elboman
Copy link
Member Author

elboman commented Apr 26, 2018

I discovered yesterday the awesome patch-package tool, and I applied the patch to enzyme-adapter-react-16 pulling it from master.

Once it gets released (last release was in January) we can remove th patch and just use the proper package.

In the meantime it works good 👍

@elboman elboman merged commit bf8162f into master Apr 26, 2018
@christopherthielen christopherthielen deleted the context-api branch May 3, 2018 02:33
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.

1 participant