-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add demo for React hooks and context (#150)
- Loading branch information
Showing
8 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["react-app"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* function Component with Hooks: Modify ./index.js to apply this file | ||
*/ | ||
import React, { useState, useLayoutEffect } from 'react'; | ||
import './App.css'; | ||
import logo from './logo.svg'; | ||
import tplsrc from './views/showing-click-times.liquid'; | ||
import Parser from 'html-react-parser'; | ||
import { engine } from './engine'; | ||
import { Context } from './Context'; | ||
import { ClickButton } from './ClickButton'; | ||
|
||
const fetchTpl = engine.getTemplate(tplsrc.toString()) | ||
|
||
export function App() { | ||
const [state, setState] = useState({ | ||
logo: logo, | ||
name: 'alice', | ||
clickCount: 0, | ||
html: '' | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
fetchTpl | ||
.then(tpl => engine.render(tpl, state)) | ||
.then(html => setState({...state, html})) | ||
}, [state.clickCount]) | ||
|
||
return ( | ||
<div className="App"> | ||
{Parser(`${state.html}`)} | ||
<Context.Provider | ||
value={{ | ||
count: () => setState({...state, clickCount: state.clickCount + 1}) | ||
}} | ||
> | ||
<ClickButton/> | ||
</Context.Provider> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
button { | ||
position: fixed; | ||
right: 20px; | ||
bottom: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import './ClickButton.css'; | ||
import React from 'react'; | ||
import { Context } from './Context'; | ||
|
||
export function ClickButton() { | ||
return ( | ||
<Context.Consumer> | ||
{context => ( | ||
<button onClick={context.count}> | ||
Click Here! | ||
</button> | ||
)} | ||
</Context.Consumer> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React from "react"; | ||
|
||
export const Context = React.createContext() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<header className="App-header"> | ||
{{ logo | image }} | ||
<h2>Welcome {{name | capitalize}}</h2> | ||
<p>Edit <code>src/App.js</code> and save to reload.</p> | ||
<p>You have clicked {{clickCount}} times.</p> | ||
<a | ||
className="App-link" | ||
href="https://reactjs.org" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn React | ||
</a> | ||
</header> |