import React, { useEffect, useState, useRef } from "react"; import Peer from "peerjs"; import { ThemeProvider, Box, Heading, Text, Label, Input, Button } from "theme-ui"; import theme from "./theme.js"; function App() { const [id, setId] = useState(""); const peer = useRef(null); useEffect(() => { peer.current = new Peer(); }, []); useEffect(() => { function handleOpen(id) { console.log("My peer ID is: " + id); setId(id); } function handleConnection(connection) { connection.on("open", () => { if (imgRef.current) { connection.send(imgRef.current); } }); } peer.current.on("open", handleOpen); peer.current.on("connection", handleConnection); return () => { peer.current.removeListener("open", handleOpen); peer.current.removeListener("connection", handleConnection); }; }, [id]); const [connectId, setConnectId] = useState(""); const connectToId = () => { const connection = peer.current.connect(connectId); connection.on("open", () => { connection.on("data", data => { const blob = new Blob([data]); imgRef.current = blob; setImgSrc(URL.createObjectURL(imgRef.current)); }); }); }; const [imgSrc, setImgSrc] = useState(""); const imgRef = useRef(null); const loadImage = e => { imgRef.current = e.target.files[0]; setImgSrc(URL.createObjectURL(imgRef.current)); }; return ( Your ID {id} { e.preventDefault(); connectToId(); }} py={3} > setConnectId(e.target.value)} /> ); } export default App;