Skip to content

Commit 5582e9b

Browse files
committed
update example
1 parent d963b85 commit 5582e9b

File tree

6 files changed

+92
-87
lines changed

6 files changed

+92
-87
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ node_modules
33
lib
44
coverage
55
.DS_Store
6-
*bundle.js
7-
package-lock.json
6+
examples/*/bundle.js
7+
examples/*/package-lock.json
8+
examples/*/yarn.lock

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const Context = createReactContext(defaultValue);
2323

2424
```js
2525
// @flow
26-
import React from 'react';
27-
import createReactContext from 'create-react-context';
26+
import React, { type Node } from 'react';
27+
import createReactContext, { type Context } from 'create-react-context';
2828

2929
type Theme = 'light' | 'dark';
3030
// Pass a default theme to ensure type correctness

examples/theme-example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<style>
77
body {
88
background-color: pink;
9+
font-family: cursive;
910
}
1011
</style>
1112
</head>

examples/theme-example/index.js

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,87 @@
11
//@flow
2-
import React from 'react';
2+
import React, { type Node } from 'react';
33
import { render } from 'react-dom';
4-
import { ThemeToggler, Title, Emoji } from './theme-context';
4+
import createReactContext, { type Context } from '../../src';
55

6-
const App = () => (
7-
<ThemeToggler>
8-
<Title>Really cool context</Title>
9-
<Emoji />
10-
</ThemeToggler>
11-
);
6+
type Theme = 'light' | 'dark';
7+
// Pass a default theme to ensure type correctness
8+
const ThemeContext: Context<Theme> = createReactContext('light');
129

13-
render(<App />, document.getElementById('container'));
10+
class ThemeToggler extends React.Component<
11+
{ children: Node },
12+
{ theme: Theme }
13+
> {
14+
state = { theme: 'light' };
15+
render() {
16+
return (
17+
// Pass the current context value to the Provider's `value` prop.
18+
// Changes are detected using strict comparison (Object.is)
19+
<ThemeContext.Provider value={this.state.theme}>
20+
<button
21+
onClick={() => {
22+
this.setState(state => ({
23+
theme: state.theme === 'light' ? 'dark' : 'light'
24+
}));
25+
}}
26+
>
27+
Toggle theme
28+
</button>
29+
{this.props.children}
30+
</ThemeContext.Provider>
31+
);
32+
}
33+
}
34+
35+
class Title extends React.Component<{ children: Node }> {
36+
render() {
37+
return (
38+
// The Consumer uses a render prop API. Avoids conflicts in the
39+
// props namespace.
40+
<ThemeContext.Consumer>
41+
{theme => (
42+
<h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
43+
{this.props.children}
44+
</h1>
45+
)}
46+
</ThemeContext.Consumer>
47+
);
48+
}
49+
}
50+
51+
class Emoji extends React.Component<{}> {
52+
render() {
53+
return (
54+
<ThemeContext.Consumer>
55+
{theme => (
56+
<div
57+
style={{
58+
fontSize: '35px',
59+
background: 'white',
60+
height: '40px',
61+
width: '40px'
62+
}}
63+
>
64+
{theme === 'light' ? '⚡️' : '🕶'}
65+
</div>
66+
)}
67+
</ThemeContext.Consumer>
68+
);
69+
}
70+
}
71+
72+
function App() {
73+
return (
74+
<ThemeToggler>
75+
<Title>Really cool context</Title>
76+
<Emoji />
77+
</ThemeToggler>
78+
);
79+
}
80+
81+
let container = document.getElementById('container');
82+
83+
if (!container) {
84+
throw new Error('missing #container');
85+
}
86+
87+
render(<App />, container);

examples/theme-example/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
22
"name": "theme-example",
33
"version": "1.0.0",
4-
"description": "Example for create react context",
4+
"description": "Example for create-react-context",
55
"main": "index.js",
66
"scripts": {
7-
"build": "webpack",
8-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"build": "webpack"
98
},
10-
"author": "",
11-
"license": "ISC",
9+
"license": "MIT",
1210
"dependencies": {
1311
"babel-core": "^6.26.0",
1412
"babel-loader": "^7.1.2",

examples/theme-example/theme-context.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)