import React, {useState, useEffect, useReducer} from 'react'; import config from './config'; import { Switch, Route, BrowserRouter as Router, Redirect } from 'react-router-dom'; import MainWrapper from './pages/Layout/MainWrapper/MainWrapper'; import { stateReducer } from './reducers/stateReducer'; import { initState } from './reducers/init/stateReducer.init'; import indexServices from './services/api/indexServices'; import './App.scss'; import socketIOClient from 'socket.io-client'; const socket = socketIOClient(config.socketAddress); function App() { const [ state, dispatch ] = useReducer( stateReducer, {}, initState ); const fetchIndexAPI = async () => { const response = await indexServices.indexService(); if (response) { const action = { type: 'INDEX', message: 'SET_USER', body: response } dispatch(action) } } useEffect(() => { fetchIndexAPI(); }, []) const socketConnect = () => { if (state.connect) return; dispatch({type:'SOCKET', message: 'LAUNCH', body:{socket, dispatch}}); } useEffect(() => { socketConnect(); }, []) return (
{state.user.username ? : }

React Boilerplate

{state.connect ? '✓' : ' ⃠'}

); } export default App;