Skip to content

Commit

Permalink
docs: add demo for React hooks and context (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 8, 2019
1 parent 3a7ddaa commit 7bae5bc
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 5 deletions.
3 changes: 3 additions & 0 deletions demo/reactjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["react-app"]
}
5 changes: 1 addition & 4 deletions demo/reactjs/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import tpl from './views/demo.liquid';
import Parser from 'html-react-parser';
import { engine } from './engine';

class App extends Component {

export class App extends Component {
async componentDidMount() {
const html = await engine.renderFile(tpl.toString(), {name: 'alice', logo: logo })
this.setState({ html }) // outputs "Alice"
Expand All @@ -22,5 +21,3 @@ class App extends Component {
);
}
}

export default App;
41 changes: 41 additions & 0 deletions demo/reactjs/src/AppWithHooks.js
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>
);
}
5 changes: 5 additions & 0 deletions demo/reactjs/src/ClickButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
button {
position: fixed;
right: 20px;
bottom: 20px;
}
15 changes: 15 additions & 0 deletions demo/reactjs/src/ClickButton.js
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>
);
};
3 changes: 3 additions & 0 deletions demo/reactjs/src/Context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from "react";

export const Context = React.createContext()
3 changes: 2 additions & 1 deletion demo/reactjs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { App } from './App';
// import { App } from './AppWithHooks';

ReactDOM.render(<App />, document.getElementById('root'));

Expand Down
14 changes: 14 additions & 0 deletions demo/reactjs/src/views/showing-click-times.liquid
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>

0 comments on commit 7bae5bc

Please sign in to comment.