Skip to content

Commit

Permalink
fix state issue in coolshape component
Browse files Browse the repository at this point in the history
  • Loading branch information
0xgreenapple committed Apr 7, 2024
1 parent 6edd2c9 commit eaf0d21
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/lib/shapes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ const Coolshape: ForwardRefExoticComponent<ShapeOptions> = forwardRef(
(options, ref) => {
const { type, index, random, ...rest } = options;

let initialShape = null;
if (index !== undefined && type) {
initialShape = shapes[type][index];
if (index !== undefined && type && !random) {
const Shape = shapes[type][index];
return <Shape {...rest} ref={ref}/>
}
const [Shape, setShape] = useState<ShapeType | null>(initialShape);

const [RandomShape, setRandomShape] = useState<ShapeType | null>(null);
useEffect(() => {
if (random || !type || index === undefined) {
const shape = getRandomShape({ type }) as ShapeType;
setShape(shape);
}
setRandomShape(shape);
}, []);
if (!Shape) {
return null;
if (!RandomShape) {
return null;
}
return <Shape {...rest} ref={ref} />;
return <RandomShape {...rest} ref={ref} />;
}
);
Coolshape.displayName = "Coolshape";
Expand Down

0 comments on commit eaf0d21

Please sign in to comment.