split icon indicating copy to clipboard operation
split copied to clipboard

(react-split): How to handle conditional children

Open addy opened this issue 3 years ago • 11 comments

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 />
    )}
</>

addy avatar May 17 '22 22:05 addy

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

adam-binks avatar May 30 '22 19:05 adam-binks

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 avatar May 30 '22 20:05 addy

@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.

addy avatar May 31 '22 16:05 addy

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 avatar May 31 '22 22:05 adam-binks

@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:)

adam-binks avatar Jun 01 '22 14:06 adam-binks

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!

addy avatar Jun 01 '22 15:06 addy

Thanks!

adam-binks avatar Jun 01 '22 16:06 adam-binks

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 <SplitWrapper> 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.

martinsvb avatar Jul 01 '22 09:07 martinsvb

@martinsvb I've hit the same issue

raymeskhoury avatar Aug 10 '22 11:08 raymeskhoury

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>

pengfeiw avatar Nov 20 '22 10:11 pengfeiw

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

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

brunohpmarques avatar Jul 03 '23 22:07 brunohpmarques