Wednesday, April 1, 2020

How to Deprecate a React Component

Deprecating a react component is a big decision, and you need to be careful not to leave our developer clients in a conundrum. To off set the pain level i have come up with some pointers that can be used to deprecate a component in a graceful way that will minimize the impact on dependent code.

Document, document, document...

It can not be overstated, you need to document your code. The first level of documentation should happen in your Wiki, newsletter, rss feed, or what have you.
It needs to be some sort of public form of communication that is official and most of your clients will see and take note of it. In this publication you should:
  • State clearly what is being deprecated
  • State why it is being deprecated
  • If there will be a replacement method, you should link to the documentation of that method
  • Give a time frame for the deprecation process
Example:
Our Notify component will be deprecated. It is being replaced with our Alert component. Please see our Alert docs for how to implement. The Notify component will be removed upon the next major release 2.0.0, we are currently in 1.4.6 release. Release 2.0 is scheduled be live during the 3rd quarter this year. We will keep you updated if anything changes on this channel.
The next place to document the changes is in the projects readme markdown file in your repository. Call out that the component will be deprecated during the next major release. Again, we will want to provide a link to any replacement components.

Finally we want to document this change in the component's code. We should place a very noticeable comment at the top of the file restating when and how this component will be deprecated.

Use Logs to notify the Developers

Having the console spit out a log in production code is generally frowned upon, but this is one of the cases that it is highly advised to use it, well sort of, you want to have some code to detect the environment and only log to the console if the application is in development and not production. With a Node environment we can do that using webpack. see the example below: 
componentDidMount() {
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn(
`The "Notice" component has been deprecated. Please considering updating to the "Alert" component. See: https://repo.url.com/jacob/alert.md`
);
}
}
view raw deprecation.js hosted with ❤ by GitHub
I hope this helps you understand the importance of having a predictable product life cycle. Now become the king of your world!!

No comments:

Post a Comment