npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-core-decorators

v1.0.2

Published

Core decorators for React project

Downloads

12

Readme

Core decorators for React project

Build Status Version Version

Useful decorators that could be utilized in React projects which use class-based definition of React components. (Keep in mind that decorators can't be used to decorate Stateless React Components).

To get more familiar with decorator pattern and its usage in ES6 read this great article.

Installation

npm i react-core-decorators -S

Usage

Next decorators are present:

mixin

Earlier while using old-fashion approach to create React components (React.createClass({...}) it was possible to use so called "mixins". At the moment, if you are defining your component as ES6 classes there is no way to use mixins natively. While usually you don't need to use them there are still some cases when you want to have them in hand. Usage is pretty straight-forward:

import PureRenderMixin from 'react-addons-pure-render-mixin';
import { mixin } from 'react-core-decorators';

@mixin(PureRenderMixin)
class Button extends React.Component { ... }

Signature

function mixin(Mixin[, Mixin][, Mixin]..) { ... }

Mixin - plain object with methods that should be added/chained to original class. You can pass multiple mixins (as multiple arguments). If mixin inclues method which is also present in other mixin or in original class prototype then they will be chained and called with proper context (this) in next order: Mixin1 -> Mixin2 -> ... -> Class prototype.

noRenderError

React is designed in such a way that if one component fails to render (uncaught exception within render method) whole app won't be rendered:

class App extends React.Component {
   render() {
       return (
           <section>
               Header
               <Body/>
               Footer
           </section>
       );
   }
};

class Body extends React.Component {
   render() {
       throw new Error();
       
       return (
           <section>Body</section>
       );
   }
};

In the example above header and footer won't be rendered due to an error inside Body component render method. To avoid above-mentioned global impact of single component on application rendering in whole use this decorator in next fashion:

import { noRenderError } from 'react-core-decorators';

@noRenderError()
class Body extends React.Component {
...

In addition this decorator shows useful stack trace to facilitate debugging process (in earlier React versions there was no easy way to understand which Component failed to render).

Signature

function noRenderError(ErrorScreen) { ... }

(optional) ErrorScreen - React Component which should be rendered instead of failed Component. It will be supplied with errorComponentName property, which is class name of failed Component. If omitted default Error Screen will be used. (Be aware, that your custom ErrorScreen component should be decorated as well, if you want to be sure it won't break rendering flow in case of error within itself).

@noRenderError()
class CustomErrorScreen extends React.Component {
    render() {
        const {
            errorComponentName
        } = this.props;
        
        return (
            <div>Oops, {errorComponentName} failed to render</div>
        );
    }
}

@noRenderError(CustomErrorScreen)
class Body extends React.Component {

Worth noting that you can't just put this decorator on some Component and expect that any errors in Child components will be handled automatically - you need to use it directly for each Child component.