material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Detail Panel Re-render on Change

Open weagertron opened this issue 7 years ago • 7 comments

I am trying to add a detailPanel to my application which will display a section which can contain multiple sets of two text inputs. These text inputs can be added and removed (not implemented yet) dynamically so that there can be essentially an infinite number of these inputs.

I am using a functional component for this with the useState hook to store the array of my inputs. This array of inputs gets passed into the TableDetail component I am using within the detailPanel render function. When this array is modified it does not trigger a re-render of the table. The only way that it can trigger a re-render is if I move the data manipulation into the render of the detailPanel itself and not use an external component. This, however, causes the entire table to re-render instead of just the detailPanel and thus collapses the detailPanel.

Is there a way to get just the detailPanel to re-render upon state/value changes?

Below is some of my code:

function TableComponent(props) {
    const blankInput = { value1: 0, value2: 0 }
    const [inputList, setInputList] = useState([blankInput]);

    const addNewInputs = () => {
        setInputList([...inputList, { ...blankInput}]);
    }

    return (
        <MaterialTable
            ...otherProps
            detailPanel={rowData => {
                return (
                    <TableDetail
                        inputList={inputList}
                        onAddClick={addNewInputs}
                    />
                )
            }}
        />
    )
}

In the supplied code I would wish a re-render to occur after the addNewInputs function is called so that the new input will be rendered within the detailPanel. In my TableDetail component I am simply mapping over the array of inputList in order to display text inputs.

Any help will be greatly appreciated!

weagertron avatar Aug 15 '19 10:08 weagertron

I managed to circumvent this issue by moving the adding/removing logic into the TableDetail component itself. It would be better if this could be handled externally though so I can create a generic component that works with dynamic functions and data which is passed into it

weagertron avatar Aug 15 '19 12:08 weagertron

I'm having the same issue right now, I need the detailPanel to re-render after I update the state that my component that is inside the detailPanel uses. Unfortunately I can't move my logic into the components itself like @weagertron did.

Is there another way to achieve the re-render? It would be good if the detailPanel could re-render the other components.

LuanScudeler avatar May 04 '20 04:05 LuanScudeler

Here is a solution which worked for me:

return (
    <MaterialTable
        ...otherProps
        detailPanel={[
          rowData => ({
            ...props,
            render: (rowData) => {
                // rowData is now updated, so changes took place
                return (
                    <TableDetail
                        inputList={inputList}
                        onAddClick={addNewInputs}/>
                );
            },
          }),
        ]}
    />
  )

ofri84 avatar Jun 17 '20 15:06 ofri84

@ofri84's solution worked for me. Had to change the row_data outside render function though

detailPanel = {[
    row_data => {
        row_data['temp'] = props.value
        return {
            tooltip: props.value,
            render: row_data => <Testview id={row_data.temp} />
        }
    }
]}
...

JimmieC avatar Jun 19 '20 13:06 JimmieC

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Sep 17 '20 13:09 stale[bot]

@ofri84's solution worked for me. Had to change the row_data outside render function though

detailPanel = {[
    row_data => {
        row_data['temp'] = props.value
        return {
            tooltip: props.value,
            render: row_data => <Testview id={row_data.temp} />
        }
    }
]}
...

I had the same problem and solved using this approach, but it's kind of hacking, it should be a proper way to work with this...

marciogurka avatar Dec 08 '20 18:12 marciogurka

This seems like an oversight and cost me many hours debugging. It has come up in other threads too: (https://github.com/mbrn/material-table/issues/1863, https://github.com/mbrn/material-table/issues/2770)

React's "reactivity" should apply in rendered detailPanels as it does anywhere else.

chris-jalon avatar Jul 28 '21 21:07 chris-jalon