0

I'm using React-Table and I have two buttons in the first cell of each row of my table and the code looks like this:

const columns = React.useMemo(
 () => [

 {
    Header: 'Available Aliens',
    columns: [
    {
        Header: 'Generate/Kill',
        className: "lifeForceRow",
        Cell: ({ row }) =>
                       <div>
                           <Button variant="success" onClick={() => 
                            handleGenerate(row.original.id)}>Generate</Button>
                           <Button variant="danger" onClick={() => 
                            handleKill(row.original.id)}>Kill</Button>
                       </div>
    },

    {
        Header: 'Planet',
        accessor: 'planetName'
    },
    {
        Header: 'Galaxy',
        accessor: 'galaxyName'
    }
            ]
        }
    ],
    []
)

And then the two functions that handle each click look like this:

const handleGenerate = (e) => {
    alert("Alien Generated");
}

const handleKill = (e) => {
    alert("Alien Killed");
}

Is there a way to remove the row when I click the Kill button and a way to highlight the row if I click the Generate button?

I have the buttons working because the alerts pop-up, but I can't figure out how to interact with the row that each button group belongs to.

Thanks!

2
  • The code snippet here is not enough to help you. Add the rest of the columns function Commented May 5, 2020 at 16:24
  • @aldokkani thanks, I added the rest of columns Commented May 5, 2020 at 19:04

1 Answer 1

1

I suppose since you are using react-table you have defined data object.
You need to modify this data object, for example filtering out the deleted row.original.id and this should re-render your component with the new rows.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes thanks I have const [data, setData] = React.useState([]); React.useEffect(() => { alienData().then(res => { setData(res.data); }); }, []);
@SkyeBoniwell Then you will need to use setData to update the rows without the deleted one.

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.