Back to home

react-hooks/

context

Version: 0.2.0

Last Updated: Unknown


React-hooks - Context

Use this tempalate to used to generate a basic context hook that throws errors without providers being used.

Required

VariableData TypeInfo
namestringContext provider name

Module files

1import * as React from 'react';
2
3const <%= _.upperFirst(_.camelCase(name)) %>Context = React.createContext();
4
5function <%= _.upperFirst(_.camelCase(name)) %>Provider(props) {
6 const [<%= _.camelCase(name) %>, set<%= _.upperFirst(_.camelCase(name)) %>] = React.useState(0);
7 const value = [<%= _.camelCase(name) %>, set<%= _.upperFirst(_.camelCase(name)) %>];
8
9 return <<%= _.upperFirst(_.camelCase(name)) %>Context.Provider value={value} {...props} />;
10}
11
12function use<%= _.upperFirst(_.camelCase(name)) %>() {
13 const context = React.useContext(<%= _.upperFirst(_.camelCase(name)) %>Context);
14 if (!context) {
15 throw new Error(`use<%= _.upperFirst(_.camelCase(name)) %> must be rendered within the <%= _.upperFirst(_.camelCase(name)) %>Provider`);
16 }
17 return context;
18}

Install


Lift generate


Repository

https://github.com/okeeffed/pkg-lift-react-hooks

Sections