Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React : multiple grids crashes #2937

Open
PlugNPush opened this issue Feb 6, 2025 · 4 comments
Open

React : multiple grids crashes #2937

PlugNPush opened this issue Feb 6, 2025 · 4 comments

Comments

@PlugNPush
Copy link

PlugNPush commented Feb 6, 2025

Hi,

I wanted to test gridstack for my project and made a very basic grid like this:

export function GridStackPage() {
  const [grid1Options] = useState(() => createGridOptions());
  const [grid2Options] = useState(() => createGridOptions());

  return (
    <div style={{ display: "flex", gap: "20px" }}>
      <div style={{ flex: 1 }}>
        <h3>Grid 1</h3>
        <GridStackProvider initialOptions={grid1Options}>
          <Toolbar gridNumber={1} />
          <GridStackRenderProvider>
            <GridStackRender componentMap={COMPONENT_MAP} />
          </GridStackRenderProvider>
        </GridStackProvider>
      </div>
      <div style={{ flex: 1 }}>
        <h3>Grid 2</h3>
        <GridStackProvider initialOptions={grid2Options}>
          <Toolbar gridNumber={2} />
          <GridStackRenderProvider>
            <GridStackRender componentMap={COMPONENT_MAP} />
          </GridStackRenderProvider>
        </GridStackProvider>
      </div>
    </div>
  );
}

Whenever I want to add a widget in them, it works fine for the last one but crashes for all the ones before (here grid 1).

The reason for the crash is :

Uncaught Error: Widget container not found for id: widget-39vq52659p5
    children grid-stack-render.tsx:55
    GridStackRender grid-stack-render.tsx:47
    React 18
    current grid-stack-render-provider.tsx:35
    renderCB grid-stack-render-provider.tsx:41
    createWidgetDivs utils.ts:128
    addWidget gridstack.ts:470
    addSubGrid grid-stack-provider.tsx:60
    onClick GridStackDemo.tsx:210

It comes from GridStackRenderProvider, in

<GridStackRenderContext.Provider
        value={useMemo(
          () => ({
            getWidgetContainer: (widgetId: string) => {
              return widgetContainersRef.current.get(widgetId) || null;
            },
          }),
          // ! gridStack is required to reinitialize the grid when the options change
          // eslint-disable-next-line react-hooks/exhaustive-deps
          [gridStack]
        )}
      >
        <div ref={containerRef}>{gridStack ? children : null}</div>
      </GridStackRenderContext.Provider>

as widgetContainersRef.current.get(widgetId) unexpectedly returns undefined.

I would appreciate some help here.

@adumesny adumesny added the React label Feb 6, 2025
@adumesny
Copy link
Member

adumesny commented Feb 6, 2025

I don't use React. I assume you are using GridStackProvider and other custom tags from the /rect example ?
I would check with those dev who worked on it @Aysnine and #2938

@PlugNPush
Copy link
Author

Yes, I copied the whole lib/ folder from the react example, and basically duplicated the grids from the first provider. I can't get the two of them to work, only the last one does.

Note that only adding a component in a grid is not working, for example, I am able to add a component in the last grid and then drag it over to the one dysfonctionning with no issues.

While trying to bypass this problem, I encountered another: in react there doesn't seem to be a fast forward way to add a component in a subgrid...

@Aysnine
Copy link
Contributor

Aysnine commented Feb 7, 2025

Hi there.
I just updated the react example and submitted the pr. Can you try to provide a minimally reproducible case in this codesandbox using the latest libs?

https://codesandbox.io/p/sandbox/github/Aysnine/gridstack-react/tree/main?file=/src/App.tsx

@adumesny
Copy link
Member

adumesny commented Feb 7, 2025

@Aysnine thanks for being active on that code. I actually wanted to start shipping them like I do for /angular
I started looking at the code and was planning to do this weekend... I wanted to match better what I did for angular wrapper (extending WidgetItem to have similar to 'selector' + 'input' which are angular centric but conceptually the same as name + props you have, rathern than JSON string in content. I'll have to think about having a custom data T type, or changing 'content'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants