From 5c97ba0f6a8acfec40e489d51a4c62b3bab4f0a1 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Thu, 18 Oct 2018 21:04:22 -0400 Subject: [PATCH 1/3] canary --- package.json | 5 +++-- src/App.js | 46 +++++++++++++++++---------------------------- src/cache.js | 17 +++++++++++++++++ src/lambda/hello.js | 2 -- yarn.lock | 27 +++++++++++++++----------- 5 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 src/cache.js diff --git a/package.json b/package.json index 94e737e..d85fc50 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,9 @@ "version": "0.4.0", "private": true, "dependencies": { - "react": "^16.5.2", - "react-dom": "^16.5.2", + "react": "^16.6.0-alpha.8af6728", + "react-cache": "^16.6.0-alpha.8af6728", + "react-dom": "^16.6.0-alpha.8af6728", "react-scripts": "2.0.4" }, "scripts": { diff --git a/src/App.js b/src/App.js index c903672..3b70476 100644 --- a/src/App.js +++ b/src/App.js @@ -1,35 +1,19 @@ -import React, { Component } from 'react'; +import React, { + unstable_ConcurrentMode as ConcurrentMode, + unstable_Suspense as Suspense, + Component +} from 'react'; import logo from './logo.svg'; import './App.css'; +import { createFetcher } from './cache'; -class LambdaDemo extends Component { - constructor(props) { - super(props); - this.state = { loading: false, msg: null }; - } - - handleClick = e => { - e.preventDefault(); - - this.setState({ loading: true }); - fetch('/.netlify/functions/hello') - .then(response => response.json()) - .then(json => this.setState({ loading: false, msg: json.msg })); - }; - - render() { - const { loading, msg } = this.state; +const DataResource = createFetcher(() => + fetch(`/.netlify/functions/hello?q=1`).then(x => x.json()) +); - return ( -

- -
- {msg} -

- ); - } +function LambdaDemo() { + const msg = DataResource.read(); + return

{JSON.stringify(msg)}

; } class App extends Component { @@ -41,7 +25,11 @@ class App extends Component {

Edit src/App.js and save to reload.

- + + + + + ); diff --git a/src/cache.js b/src/cache.js new file mode 100644 index 0000000..044877d --- /dev/null +++ b/src/cache.js @@ -0,0 +1,17 @@ +// cache.js +import { createCache, createResource } from 'react-cache'; + +export let cache; +function initCache() { + cache = createCache(initCache); +} +initCache(); + +export function createFetcher(callback) { + const resource = createResource(callback); + return { + read(...args) { + return resource.read(cache, ...args); + } + }; +} diff --git a/src/lambda/hello.js b/src/lambda/hello.js index 49b0bc8..9cb06b0 100644 --- a/src/lambda/hello.js +++ b/src/lambda/hello.js @@ -4,8 +4,6 @@ const obj = { }; export function handler(event, context, callback) { console.log('qsp', event.queryStringParameters); - console.log('**********'); - console.log('mvsp', event.multiValueQueryStringParameters); callback(null, { statusCode: 200, body: JSON.stringify({ msg: 'Hello, World!', ...obj }) diff --git a/yarn.lock b/yarn.lock index ef5c115..05498f0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6885,6 +6885,10 @@ react-app-polyfill@^0.1.3: raf "3.4.0" whatwg-fetch "3.0.0" +react-cache@^16.6.0-alpha.8af6728: + version "16.6.0-alpha.8af6728" + resolved "https://registry.yarnpkg.com/react-cache/-/react-cache-16.6.0-alpha.8af6728.tgz#b0a9d1d1c598245540032fa6e2ab4bbcb22ae840" + react-dev-utils@^6.0.4: version "6.0.4" resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-6.0.4.tgz#cd6d2712aa22d67751ef6757dc82787351da8826" @@ -6912,14 +6916,14 @@ react-dev-utils@^6.0.4: strip-ansi "4.0.0" text-table "0.2.0" -react-dom@^16.5.2: - version "16.5.2" - resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.5.2.tgz#b69ee47aa20bab5327b2b9d7c1fe2a30f2cfa9d7" +react-dom@^16.6.0-alpha.8af6728: + version "16.6.0-alpha.8af6728" + resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.6.0-alpha.8af6728.tgz#0bc2020dcf0a594fe06e41379a9346d1982f587b" dependencies: loose-envify "^1.1.0" object-assign "^4.1.1" prop-types "^15.6.2" - schedule "^0.5.0" + scheduler "^0.10.0-alpha.8af6728" react-error-overlay@^5.0.4: version "5.0.4" @@ -6978,14 +6982,14 @@ react-scripts@2.0.4: optionalDependencies: fsevents "1.2.4" -react@^16.5.2: - version "16.5.2" - resolved "https://registry.yarnpkg.com/react/-/react-16.5.2.tgz#19f6b444ed139baa45609eee6dc3d318b3895d42" +react@^16.6.0-alpha.8af6728: + version "16.6.0-alpha.8af6728" + resolved "https://registry.yarnpkg.com/react/-/react-16.6.0-alpha.8af6728.tgz#b97b6453c252a1f6ad185acc81aca1485cd3e120" dependencies: loose-envify "^1.1.0" object-assign "^4.1.1" prop-types "^15.6.2" - schedule "^0.5.0" + scheduler "^0.10.0-alpha.8af6728" read-pkg-up@^1.0.1: version "1.0.1" @@ -7386,10 +7390,11 @@ saxes@^3.1.3: dependencies: xmlchars "^1.3.1" -schedule@^0.5.0: - version "0.5.0" - resolved "https://registry.yarnpkg.com/schedule/-/schedule-0.5.0.tgz#c128fffa0b402488b08b55ae74bb9df55cc29cc8" +scheduler@^0.10.0-alpha.8af6728: + version "0.10.0-alpha.f47a958" + resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.10.0-alpha.f47a958.tgz#0063a5c21aad2038111c316e417e9702d1ca7007" dependencies: + loose-envify "^1.1.0" object-assign "^4.1.1" schema-utils@^0.4.4, schema-utils@^0.4.5: From e122dc4c73348c87fa4d52c573ec2173a2a19cec Mon Sep 17 00:00:00 2001 From: sw-yx Date: Thu, 18 Oct 2018 21:26:26 -0400 Subject: [PATCH 2/3] commit --- src/App.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/App.js b/src/App.js index 3b70476..8c2c965 100644 --- a/src/App.js +++ b/src/App.js @@ -19,19 +19,19 @@ function LambdaDemo() { class App extends Component { render() { return ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - + +
+
+ logo +

+ Edit src/App.js and save to reload. +

+ - -
-
+
+
+ ); } } From d7ffc0cf8cfbe813695c19b6b4c326af517d8281 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Thu, 18 Oct 2018 21:37:59 -0400 Subject: [PATCH 3/3] msg --- src/App.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/App.js b/src/App.js index 8c2c965..f477a1f 100644 --- a/src/App.js +++ b/src/App.js @@ -8,12 +8,12 @@ import './App.css'; import { createFetcher } from './cache'; const DataResource = createFetcher(() => - fetch(`/.netlify/functions/hello?q=1`).then(x => x.json()) + fetch(`/.netlify/functions/hello`).then(x => x.json()) ); function LambdaDemo() { - const msg = DataResource.read(); - return

{JSON.stringify(msg)}

; + const msg = DataResource.read().msg; + return

{msg}

; } class App extends Component {