Search code examples
javascriptreactjsanimationreact-hooksreact-pose

simple react-pose opacity transition dont work


i'm new with react-pose and i try a simple thing but the transition dont work.

I only want to have a transition between 2 states.

Like opacity 0 => 1

I want to use it with a const so i use the new react hook.

import React, { useState } from 'react';
import posed from 'react-pose';

const Pop = () => {
  const [position, setPosition] = useState(false);
  const Box = posed.div({
    left: { x: 0 },
    right: { x: 100 }
  });
  const toggleVisibility = () => {
    setPosition(!position);
  };

  return (
    <div>
      <Box pose={position ? 'left' : 'right'} className="box" />
      <button onClick={toggleVisibility}>Toggle visibility</button>
    </div>
  );
};

export default Pop;

Everything is working but this code act like i have set transition: 0s

Can you help me ?


Solution

  • Here the solution You need to put the const Box outside the react class, for prevent rerender.

    import React, { useState } from 'react';
    import posed from 'react-pose';
    
    const Box = posed.div({
        left: { x: 0 },
        right: { x: 100 }
      });
    
    const Pop = () => {
      const [position, setPosition] = useState(false);
      const toggleVisibility = () => {
        setPosition(!position);
      };
    
      return (
        <div>
          <Box pose={position ? 'left' : 'right'} className="box" />
          <button onClick={toggleVisibility}>Toggle visibility</button>
        </div>
      );
    };
    
    export default Pop;