Skip to content

nikovanmeurs/react-as-hoc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React as HOC

Transform React components accepting a render prop into a higher-order components.

Installation

npm install react-as-hoc

Usage

Take the following pattern in which a computed value is passed from a Provider component to its render prop:

const GreetingProvider = ({ children, name }) => {
  const greeting = name ? `Hello ${name}!` : "Hello!";
  return children({ greeting });
};

const GreetingPresenter = ({ greeting }) => <h1>{greeting}</h1>;

const GreetTheWorld = () => (
  <GreetingProvider name="World">{({ greeting }) => (
    <GreetingPresenter greeting={greeting}>
  )}
);

Using react-as-hoc, this can alternatively be written as:

import asHoc from 'react-as-hoc';

const GreetingProvider = ({ children, name }) => {
  const greeting = name ? `Hello ${name}!` : "Hello!";
  return children({ greeting });
};

const GreetingPresenter = ({ greeting }) => <h1>{greeting}</h1>;

const withGreeting = asHoc(GreetingProvider);
const GreetTheWorld = withGreeting({ name: "World" })(GreetingPresenter);

About

Transform React components accepting a render prop into higher-order components.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published