poweratom
poweratom

Reputation: 2434

Trouble using a React component from an older version of React in v16.x

I'm relatively new to React but have found it productive in the past week. But I ran into sort of a conundrum.

I built a new project off of create-react-app CLI, which has the latest React v16.x and related packages included. However, the project I'm working on also requires an in-house package used by many other internal projects (hence I can't just go change things around). And it's built with React v15.x.

Here's the problem: When I import a component from that in-house package, I ran into the issue of React v16 having deprecated React.PropTypes syntax (which is still available in React v15.x).

TypeError: Cannot read property 'oneOf' of undefined

The context of above undefined being React.PropTypes.oneOf(...) in the legacy v15 React module.

So how do I have the best of both worlds? Continue with v16.x on the project while being able to leverage components in packages from another era (v15.x)?

Thanks!

Upvotes: 1

Views: 777

Answers (1)

Sam R.
Sam R.

Reputation: 16450

So here's the answer based on the comment I left:

For the most part it's a safer choice if you stick with react 15.x until the code you depend on is updated to 16.x as well. Just make sure you use prop-types package in your own code so you don't have to do the same migration again later.

For the legacy code you can start installing the prop-types package and change their schema to use that. Instead of:

import React from 'react';

Something.propTypes = {
  name: React.PropTypes.string
};

Use:

import PropTypes from 'prop-types';

Something.propTypes = {
  name: PropTypes.string
};

This should be very easy with a simple find/replace and your team and the maintainers will thank you :)

Upvotes: 3

Related Questions