0

I'm using react-table (well...) to handle all of my tables in my app.

Basically, I'm trying to think of a way to maintain the CSS transitions in custom cells when the data of the table is changed. I'll give you an example:

I have a table of posts. Each post has an "active" column. This column is a UI switch. When the value of the cell (boolean) is true, the switch is turned on, and when it's off, then it toggles off. Whenever I toggle on or off the switch, there's a small animation that occurs, but not in react-table and I think I know why.

I assume, that whenever the data of the table is changed, then each Cell callback is being invoked and it returns a whole new component each time. If I'm wrong (hopefully), what would be the best approach here?

TL;DR - "Why it's not working"

I have created a demo here - https://codesandbox.io/s/brave-mendeleev-lkf5d?file=/src/index.css

This is the expected outcome:

A box with a background transition

This is the actual outcome:

a table with boxes that won't transition

Update

I'm looking for a solution that will update the state while doing the transitioning.

1 Answer 1

1

You should make that transition piece into its own element that manages its own state:

const TransitionDiv = () => {
  const [active, setActive] = React.useState(false);
  return (
    <div
      className={"switch " + (active ? "active" : "inactive")}
      onClick={() => setActive(!active)}
    />
  );
};

Then use it like this:

...
          {
            Header: "Switch",
            accessor: "isActive",
            Cell: ({ value, cell: { row } }) => <TransitionDiv />
          }
...

Sandbox: https://codesandbox.io/s/admiring-hamilton-ylqtj?file=/src/App.js

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

4 Comments

But it's uncontrolled. It doesn't update the actual state... the state of the div should be derived from the props. That's the whole purpose of my goal.
@EliyaCohen I said ...that manages its own state not to suggest that that was the culprit. I only said it to complete the point; that it should be its own element. This same issue happens inside MUI elements a lot. Encapsulating these transitional components into their elements seems to always fix it.
I understand, and while I thank you for trying to help, this is not a solution and it completes the transition "point" by breaking the functional one. I can't mark this question as an answer, and I don't mean to sound rude but I would prefer if you could modify your answer so it won't break the functional logic, or delete it. Thanks :)
@EliyaCohen Fair enough.

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.