Skip to content

Commit eaf0d21

Browse files
committed
fix state issue in coolshape component
1 parent 6edd2c9 commit eaf0d21

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/lib/shapes.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@ const Coolshape: ForwardRefExoticComponent<ShapeOptions> = forwardRef(
2020
(options, ref) => {
2121
const { type, index, random, ...rest } = options;
2222

23-
let initialShape = null;
24-
if (index !== undefined && type) {
25-
initialShape = shapes[type][index];
23+
if (index !== undefined && type && !random) {
24+
const Shape = shapes[type][index];
25+
return <Shape {...rest} ref={ref}/>
2626
}
27-
const [Shape, setShape] = useState<ShapeType | null>(initialShape);
27+
28+
const [RandomShape, setRandomShape] = useState<ShapeType | null>(null);
2829
useEffect(() => {
29-
if (random || !type || index === undefined) {
3030
const shape = getRandomShape({ type }) as ShapeType;
31-
setShape(shape);
32-
}
31+
setRandomShape(shape);
3332
}, []);
34-
if (!Shape) {
35-
return null;
33+
if (!RandomShape) {
34+
return null;
3635
}
37-
return <Shape {...rest} ref={ref} />;
36+
return <RandomShape {...rest} ref={ref} />;
3837
}
3938
);
4039
Coolshape.displayName = "Coolshape";

0 commit comments

Comments
 (0)