UNPKG

pixi-fusion

Version:

This module offers a set of common components needed for playing games.

30 lines (29 loc) 1.1 kB
import React, { useCallback, useEffect, useMemo, useState } from "react"; import { StageContext } from "./Stage.context"; import { useWorld } from "../hooks"; import { Layer } from "../layer"; export const Stage = ({ children }) => { const { application } = useWorld(); const [things, setThings] = useState([]); const addObject = useCallback((thing) => { setThings((oldThings) => [...oldThings, thing]); }, [things]); const removeObject = useCallback((thing) => { setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid)); }, [application]); const conextValue = useMemo(() => ({ addObject, removeObject }), [addObject, removeObject]); useEffect(() => { if (!application) { return; } application.stage.addChild(...things); return () => { application.stage.removeChild(...things); }; }, [things, application]); return (React.createElement(StageContext.Provider, { value: conextValue }, React.createElement(Layer, null, children))); };