Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/ pure-component Public archive

Create React components that favor pure render functions and immutable props.

Notifications You must be signed in to change notification settings

RealGeeks/pure-component

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pure Component

Create React components that favor pure render functions and immutable props.

Usage

Using a pure render function:

var React = require('react');
var ReactDom = require('react-dom');
var pureComponent = require('pure-component');

var myComponent = pureComponent(function myComponentRender(props) {
  return React.DOM.p(null, props.text);
});

ReactDom.render(myComponent({text: 'Hello'}), document.body);

Using a spec object:

var React = require('react');
var ReactDom = require('react-dom');
var pureComponent = require('pure-component');

var myComponentSpec = {
  displayName: 'My Component',
  contextTypes: {
    greeting: React.PropTypes.string
  },
  render: function (props, context) {
    return React.DOM.p(null, context.greeting + ' ' + props.children);
  }
};

var myComponent = pureComponent(myComponentSpec);

var myAppSpec = {
  childContextTypes: {
    greeting: React.PropTypes.string
  },
  getChildContext: function () {
    return {greeting: 'Hello'};
  },
  render: function (props) {
    return myComponent(props.children);
  }
};

var myApp = pureComponent(myAppSpec);

React.render(myApp('World'), document.body);

Spec

When passing in a spec object all of it’s own properties other than contextTypes, defaultProps, displayName and propTypes are added to the created component’s prototype. The mentioned props get added directly on the constructor instead.

Unless overwritten from spec, components default to using the pure render shouldComponentUpdate.

The render function receives props and an optional context as arguments.

Display name

The component display name is taken either from the spec, or the displayName or name of the render function, in that order.

About

Create React components that favor pure render functions and immutable props.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published