diff --git a/demo/reactjs/.eslintrc.json b/demo/reactjs/.eslintrc.json new file mode 100644 index 000000000..fcf5ef32e --- /dev/null +++ b/demo/reactjs/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": ["react-app"] +} diff --git a/demo/reactjs/src/App.js b/demo/reactjs/src/App.js index 59944d7da..008742623 100644 --- a/demo/reactjs/src/App.js +++ b/demo/reactjs/src/App.js @@ -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" @@ -22,5 +21,3 @@ class App extends Component { ); } } - -export default App; diff --git a/demo/reactjs/src/AppWithHooks.js b/demo/reactjs/src/AppWithHooks.js new file mode 100644 index 000000000..68b788e6a --- /dev/null +++ b/demo/reactjs/src/AppWithHooks.js @@ -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 ( +
Edit src/App.js and save to reload.
You have clicked {{clickCount}} times.
+ + Learn React + +