1

I have a table which is a Table component from Material UI. I fetch data from an API and then display the data in the table. The table updates all rows based on events received from web socket. My requirement is when a row say with ID 10 has one of its row values as Closed, I want to remove that particular row from the table. Im using an array to store the data from API and then using map() to render them in individual rows.

    mapTableData = (TableData) => {
const newTableData = tempTableData.map((item, index) => ({
  ...item,
  Column1: () =>
    <p>
      {item.parameter1}
    </p>,
  Column2: () =>
   ........

When an event arrives on web socket and the value of {item.parameter1} changes to 'Closed' I want to remove that particular row from the table. How do implement this? I tried using conditional check for every column but it just gives me a blank row and wont remove it.

1 Answer 1

1

You can use filter which will iterate over the items twice:

const render = (tableData) => {
    return tableData
        .filter(row => row.id !== 10)
        .map(row => {
            // render logic
        })
}

Or, more efficiently, use reduce:

const render = (tableData) => {
    return tableData
        .reduce((aggr, row) => {
            if (row.id !== 10) {
                const jsx = ''; // some logic
                aggr.push(jsx);
            }

            return aggr;
        }, []);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.