React Redux + Socket.IO + Create React App - reactjs

I want to start by saying this is my first project with React Redux and Socket.IO.
I also have no knowledge of what it takes to construct proper build, start, and test scripts. My knowledge of webpack is also pretty limited to the point where I only really feel comfortable adding or removing loaders. I find a lot of the pre-configurations in create react app to be very helpful; jest, hot loading, build outputs. As you know react-create-app uses webpackdev server. However, for the server side portion of my app I have a socket.io server similar to this configuration.
import Server from 'socket.io';
export function startServer(store) {
const io = new Server().attach(8090);
store.subscribe(
() => io.emit('state', store.getState().toJS())
);
io.on('connection', (socket) => {
socket.emit('state', store.getState().toJS());
socket.on('action', store.dispatch.bind(store));
});
}
Basically I am looking for a resource or even better an explanation that can help me incorporate socket io and redux with the best parts of react-create-app.

I ended up getting rid of webpack entirely and removed the scripts. I rewrite the test script to a simple jest command. Followed this to finish up the socket.io config. https://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html
You can also see how the application looks like right now. https://github.com/donleyac/react-card-engine

For the client side I suggest to manage the project following this guide.
Redux guide
For the socket.io integration I suggest using my two redux middlewares that allows you to subscribe events and emit messages using the redux paradigm.
socket.io-emitter-middleware
socket.io-subscriber-middleware
You can install them via the following commands
npm i socket.io-emitter-middleware
npm i socket.io-subscriber-middleware
and following the README from github documentation for the usage.
The server part could go well.
You must not confuse the create-react-app environment with the client/server architecture. The create-react-app environment is only "client side".
You need connect your client react app with the node.js server via socket.io.
I suggest you to start from the socket.io documentation.
Socket.io Introduction

Related

Nest.js / Server Side rendering with React and assests

There will be no code cause I'm a little bit misunderstood the core principle of server rendering I guess.
I have:
Nest.js with configured Handlebars. It's built with TypeScript out of the box. I don't use webpack here.
I also created a standalone folder with a client part of my application. There are React, webpack etc configured and working.
I need to create a server rendering. For this purpose, I use ReactDOMServer. Everything works unless it bumps into SVG components and CSS modules.
And here goes my question - how to deal with it?
What's the proper way? Should I replicate webpack config on my server side (I mean for assets), or is there a way to make it works with TypeScript (npm run build)?
Thank you for your help!
You have to serve the assets files https://docs.nestjs.com/recipes/serve-static
you have to install #nestjs/serve-static
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, 'directory') // path to your files,
}),
],

REACTJS.NET SSR - Object doesn't support property or method 'forwardRef'

I am trying to render my REACT components via SSR with REACTJS.NET in ASP.NET project.
In the JS file for SSR, I'm importing SimpleBar component from simplebar-react package.
This is causing the error TypeError: Object doesn't support property or method 'forwardRef'.
I currently have 2 JS files, one for server and one for client. In the JS for server I am removing the adding of event listeners and similar. However, I can't get away from importing at least the npm package in both JS files.
Any idea on how I can avoid such error?
I am using Webpack + REACTJS.NET version 3.2.0.
So, after trying a lot of things, this is the best solution I came across.
Before I begin, I am aware that conditional imports is being introduced in ECMA but it isn't working for me, or at least, they way I have the project setup.
Basically, my solution is resolved by mixing ES6 with CommonJS and with help of Webpack and babel.
In Webpack I am creating a plugin:
And in code, when I want to import the Simplebar react component, I do the following in the constructor:
And then, whenever I want to use my imported component, I do the following:
This was the best way I found in order to render with SSR. I think it's okay since the content between server and client side are the same.
Does anyone know a better solution? Or do you have concerns?

Several instances of 'styled-components' initialized in CRA micro-frontend application

I'm running a micro frontend application with multiple React versions, each micro frontend repo is using lazy loading for dynamically loading its React version and it works as expected (Yay!)
The micro frontend app is structured as follows:
That being said, I am experiencing issues with the styled-components multi-versioning, and receiving the following console warning in dev mode (not in production):
For more technical details check out the example repo with the implementation.
Since the application is using CRA to simplify the configurations of webpack/babel I was wondering if there is a good way to improve the initial configuration to resolve this console warning.
I checked the docs link shared on the warning, and while I understand micro frontend wouldn't be the best way to maintain your project, we'd still like to provide this option to our users as they might need to gradually migrate their project versions, so I'm still interested in getting this console warning resolved.
Any tips or suggested solutions would be appreciated.
printed warning:
react_devtools_backend.js:2430 It looks like there are several instances of 'styled-components' initialized in this application. This may cause dynamic styles to not render properly, errors during the rehydration process, a missing theme prop, and makes your application bigger without good reason.
See https://s-c.sh/2BAXzed for more info.
I was facing the same issue and I solved it by moving all my styled components inside remote apps and exposed them from there.
I had the same issue with my micro-service application. For each micro-service I had package.json file and one global package.json for all micro-service. So, in the global package.json I added resolution, it allowed me to have only one version of styled component for my app
"resolutions": { "styled-components": "4.2.1" },
Also, I used to lerna. It can help you with your micro-service's dependencies.
I hope my answer can help you resolve your problem.

Getting Parcel Bundler Code Splitting working with React + TypeScript + MobX + ReactRouter

I've been trying to get Parcel Bundler to code split a ReactJS project for a while with no success, this project also used TypeScript, ReactRouter and MobX for its state management.
I've been meaning to put a boilerplate sample together and ask for help here for a while but not got round to it.
After a quick google earlier today, I came across a ready made boilerplate on Github which uses the same setup and also does not code split...
https://github.com/wenpengfei/parcel-typescript-react-boilerplate
Once compiled and ran, it just ends up with one big .js file in the dist folder, the idea behind code splitting is to create several smaller .js files per 'area' of the web application right?
Is this even possible with this combination of libraries/frameworks? If not, is my only option to go down the webpack route instead (if that will work?)?
For client side code splitting just use import + React.lazy. Should work out of the box.
To support server-side code splitting (or client-side code splitting with server-side rendering) - https://github.com/theKashey/react-imported-component is the only choice for today.

React Native Debugger's connect to React not working with Expo Client

[Update on March 20, 2019: If you have this issue, check out my post here!]
I'm making a mobile app using Expo and React Native.
I would like to integrate React Native Debugger to my project for react and redux debugging. But am having trouble.
Specifically connecting to react devtools have failed. I think this is because I am using Expo client (real device connected via wifi).
According to the docs, I should follow this path.
Since my Expo version is 25.0.0, the RN version is 0.52. Therefore, I must edit the setupDevtools.js.
The problem is, this doc is confusing to me. And I'm not sure whether I'm doing the right thing. So far, as below, I have changed the host to my IP address. But this didn't work. I guess I'm doing something wrong. Could anyone help? Thanks in advance.
const host = PlatformConstants && PlatformConstants.ServerHost ?
PlatformConstants.ServerHost.split(':')[0] :
'localhost';
reactDevTools.connectToDevTools({
isAppActive,
host: "116.0.157.7",
// Read the optional global variable for backward compatibility.
// It was added in https://github.com/facebook/react-native/commit/bf2b435322e89d0aeee8792b1c6e04656c2719a0.
port: window.__REACT_DEVTOOLS_PORT__,
resolveRNStyle: require('../../StyleSheet/flattenStyle'),
});

Resources