nathancahill / split

Unopinionated utilities for resizeable split views
https://split.js.org/
MIT License
6.11k stars 449 forks source link

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

Open addy opened 2 years ago

addy commented 2 years ago

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 commented 2 years ago

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

addy commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

Great ok! I spent a couple of hours on this afternoon and I think I tracked down the part to change - not at my computer at the moment but tomorrow I'll share my work so far. I'm optimistic that this will be a small change!

adam-binks commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

Thanks!

martinsvb commented 2 years ago

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 commented 2 years ago

@martinsvb I've hit the same issue

pengfeiw commented 1 year ago

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 commented 1 year ago

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

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