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-split): How to handle conditional children #744

Open
addy opened this issue May 17, 2022 · 11 comments · May be fixed by #746
Open

(react-split): How to handle conditional children #744

addy opened this issue May 17, 2022 · 11 comments · May be fixed by #746

Comments

@addy
Copy link

addy commented May 17, 2022

I've got an issue where the entire Split system isn't being initialized (no gutter present) if the second of my two children is being conditionally rendered.

My second child is rendered asynchronously based on some logic, I would like for the Split system to initialize with the gutter once the second child is rendered.

Basic example would look like this:

<Split>
    <ComponentA />
    {shouldRender && (
        <ComponentB />
    )}
</Split>

Is it possible to do the above without having to do something like this:

<>
    {shouldRender ? (
        <Split>
            <ComponentA />
            <ComponentB />
        </Split>
    ) : (
        <ComponentA />
    )}
</>
@adam-binks
Copy link

This might be related to the issue I'm having #745. Did you find a solution to this in the end?

@addy
Copy link
Author

addy commented May 30, 2022

I spent a bit of time going into the lib and trying to force it to re-trigger a render. I think if I spent some time actually debugging the code it could be figured out, but for the time being I essentially did my second example.

Don't do conditionals within the Split, but conditionally render the entire Split, with all of its children present.

@addy
Copy link
Author

addy commented May 31, 2022

@adam-binks So, I don't think that this is supported out of the box, but I don't think it's a bad idea. I'll see if I can't get a PR open this week to handle both of our cases.

@adam-binks
Copy link

adam-binks commented May 31, 2022 via email

@adam-binks adam-binks linked a pull request Jun 1, 2022 that will close this issue
@adam-binks
Copy link

@addy The changes in this PR seem to fix this issue. It works but I am seeing an error:
image

I don't have time to investigate this right now but this might be a useful starting point for you if you look into it:)

@addy
Copy link
Author

addy commented Jun 1, 2022

At first glance that looks a lot like what I had in mind, I wasn't sure if we needed to call this.split.destroy with true, false to not preserve the gutter.

I'll see if I can't find that proptype issue!

@adam-binks
Copy link

Thanks!

@martinsvb
Copy link

I'm encounter similar issue.
We have initial screen with 1 split view and sizes: [100].
There is requirement dynamical split view adding / removing.
So, if I add second split view and sets sizes prop: [50, 50] dynamically, error is thrown.
Console error:
split.es.js:719 Uncaught TypeError: Cannot read properties of undefined (reading 'a') at split.es.js:719:1 at Array.forEach (<anonymous>) at Object.setSizes (split.es.js:715:1) at SplitWrapper.componentDidUpdate (react-split.es.js:108:1) at commitLayoutEffectOnFiber (react-dom.development.js:23232:1) at commitLayoutMountEffects_complete (react-dom.development.js:24578:1) at commitLayoutEffects_begin (react-dom.development.js:24564:1) at commitLayoutEffects (react-dom.development.js:24502:1) at commitRootImpl (react-dom.development.js:26779:1) at commitRoot (react-dom.development.js:26638:1)

`react_devtools_backend.js:4026 The above error occurred in the component:

at SplitWrapper (http://localhost:3000/static/js/bundle.js:178736:16)
at Items (http://localhost:3000/static/js/bundle.js:78293:77)
at Outlet (http://localhost:3000/static/js/bundle.js:178553:26)
at div
at http://localhost:3000/static/js/bundle.js:80768:66
at Container (http://localhost:3000/static/js/bundle.js:94477:82)
at main
at http://localhost:3000/static/js/bundle.js:80768:66
at Content (http://localhost:3000/static/js/bundle.js:83142:5)
at Root (http://localhost:3000/static/js/bundle.js:85300:19)
at http://localhost:3000/static/js/bundle.js:77513:77
at Routes (http://localhost:3000/static/js/bundle.js:178645:5)
at Router (http://localhost:3000/static/js/bundle.js:178578:15)
at BrowserRouter (http://localhost:3000/static/js/bundle.js:177387:5)
at I18nextProvider (http://localhost:3000/static/js/bundle.js:174018:19)
at LocalisationProvider (http://localhost:3000/static/js/bundle.js:78033:5)
at SnackbarProvider (http://localhost:3000/static/js/bundle.js:142916:24)
at InnerThemeProvider (http://localhost:3000/static/js/bundle.js:111896:70)
at ThemeProvider (http://localhost:3000/static/js/bundle.js:111598:5)
at ThemeProvider (http://localhost:3000/static/js/bundle.js:111916:5)
at MuiProviders (http://localhost:3000/static/js/bundle.js:78117:5)
at Provider (http://localhost:3000/static/js/bundle.js:174711:5)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.`

There is probabably problem, that Split parent component operates with new sizes array over previous element settings and can't find new split elements added as children.

@raymeskhoury
Copy link

@martinsvb I've hit the same issue

@pengfeiw
Copy link

You can add a key prop to force Split rerender when child change.

            <Split className="split" key={panes.length}>
                {
                    panes.map((pane) => (
                        <div className="split-view-pane" key={pane.key}>
                            {pane.ele}
                        </div>
                    ))
                }
            </Split>

@brunohpmarques
Copy link

I used the key prop like @pengfeiw suggested and it worked fine 👍

<Split className="split" key={panes.length}>
...
</Split>

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

Successfully merging a pull request may close this issue.

6 participants