From 65d70da09957eb2fe94158519b75baba3e5d43eb Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Thu, 10 Dec 2020 06:08:57 +0200 Subject: [PATCH 1/3] Added example using a controlled React component and hooks. #735 --- demo/index.html | 1 + demo/react-hooks.html | 94 +++++++++++++++++++++++++++++++++++++++++++ doc/CHANGES.md | 2 +- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 demo/react-hooks.html diff --git a/demo/index.html b/demo/index.html index a47b5fa53..e30649bd0 100644 --- a/demo/index.html +++ b/demo/index.html @@ -15,6 +15,7 @@

Demos

  • Nested grids
  • Responsive
  • ReactJS
  • +
  • ReactJS (Hooks)
  • Right-To-Left (RTL)
  • Serialization
  • Static
  • diff --git a/demo/react-hooks.html b/demo/react-hooks.html new file mode 100644 index 000000000..490ca339b --- /dev/null +++ b/demo/react-hooks.html @@ -0,0 +1,94 @@ + + + + + + + Gridstack.js React integration example + + + + + + + + + + +
    + + + + + \ No newline at end of file diff --git a/doc/CHANGES.md b/doc/CHANGES.md index d7533b8db..831d950ed 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -5,7 +5,7 @@ Change log **Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)* -- [3.1.2 (2020-12-7)](#311-2020-12-7) +- [3.1.2 (2020-12-7)](#312-2020-12-7) - [3.1.0 (2020-12-4)](#310-2020-12-4) - [3.0.0 (2020-11-29)](#300-2020-11-29) - [2.2.0 (2020-11-7)](#220-2020-11-7) From 82e5525677e5756c4fe72aa385d5315698af78de Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Thu, 10 Dec 2020 11:03:51 +0200 Subject: [PATCH 2/3] Updated in response to feedback on #1531 --- demo/react-hooks.html | 156 +++++++++++++++++++++++++++++++++--------- 1 file changed, 125 insertions(+), 31 deletions(-) diff --git a/demo/react-hooks.html b/demo/react-hooks.html index 490ca339b..6272b40ec 100644 --- a/demo/react-hooks.html +++ b/demo/react-hooks.html @@ -15,7 +15,28 @@ -
    +
    +

    Using GridStack.js with React hooks

    +

    + As with any virtualDOM-based framework, you need to check if Reacthas rendered the DOM (or any updates to it) + before you initialize GridStack or call its methods. This example shows how to make rendered + components widgets: +

    +
      +
    1. Render items, each with a reference
    2. +
    3. Convert each rendered item to a widget using the reference and the + makeWidget function
    4. +
    +
    +
    +

    Controlled stack

    +
    +
    +
    +

    Uncontrolled stack

    +
    +
    \ No newline at end of file From a7eac93185d0ce8ac7bf84fed0aeb82e2311e9be Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Thu, 10 Dec 2020 11:12:40 +0200 Subject: [PATCH 3/3] Fixed state-management bug where old state values were set instead of new state values --- demo/react-hooks.html | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/demo/react-hooks.html b/demo/react-hooks.html index 6272b40ec..e3c8e6fd9 100644 --- a/demo/react-hooks.html +++ b/demo/react-hooks.html @@ -138,18 +138,18 @@

    Uncontrolled stack

    grid.on('dragstop', (event, element) => { const node = element.gridstackNode - setState({ - ...state, + setState(prevState => ({ + ...prevState, info: `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`, - }) + })) let timerId window.clearTimeout(timerId) timerId = window.setTimeout(() => { - setState({ - ...state, + setState(prevState => ({ + ...prevState, info: '', - }) + })) }, 2000) }) }, []) @@ -167,7 +167,7 @@

    Uncontrolled stack

    } node.id = node.content = String(state.count) setState(prevState => ({ - ...state, + ...prevState, count: prevState.count + 1, })) grid.addWidget(node) @@ -175,6 +175,7 @@

    Uncontrolled stack

    > Add Widget +
    {JSON.stringify(state)}
    )