Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6581

Is it good practice to implement my Socket.io within a React Context?

$
0
0

I need my socket to be accessible across all of the components within my application. My original idea was to implement the socket within a react context and then wrapping the entire application within the Socket Context I've created. I was just wondering if this is a good way to do it or if there are better implementations of this?

This is what I have so far...

import React, { createContext, useContext } from "react";import { io, Socket } from "socket.io-client";const SocketContext = createContext<Socket | null>(null);export const useSocket = () => {  return useContext(SocketContext);};export const SocketProvider = ({ children }: { children: React.ReactNode }) => {  const socket = io("http://localhost:3000");  return (<SocketContext.Provider value={socket}>{children}</SocketContext.Provider>  );};

Viewing all articles
Browse latest Browse all 6581

Latest Images

Trending Articles



Latest Images