Archie  Clayton

Archie Clayton


React Hooks + RxJS Subjects

I remember when I first got introduced to React. I was stunned by how great the idea of handling all of the presentation logic with just Javascript and JSX was, and even more so: how effortless it seemed at first–you pass in props, update states. How easy!

Going through the first Tic-Tac-Toe tutorial on the React homepage felt so great, I was confident that humanity has finally found the magic bullet, at least for the front-end.

But… there was a caveat. What if we lifted the state five floors up, but some of that state and state update logic was still needed on the first floor?

How about we pass some state one level down, then some of that state one level further and so on till it reaches the component? That’s a bit laborious but ok.

Oh, I forget, I need to change the shape of the state on the fifth floor… and props on the fourth… and props on the third… second… first…

Ouch! Is it by mistake or design? Turns out this state-to-props issue is by design, and this is where Redux steals the show. However, five minutes into the Redux tutorial you begin to realize that your excitement about React was an overreaction… or was it?

There were many attempts I know of to steal the show back for React, to name a few: React Context API as well as various state managers; but for me, none of them got the excitement I initially had for React back… except when I learned about RxJS.


This article’s main goal is to justify that the RxJS and React combo allows for better readability, less boilerplate and being closer to React while allowing the same functionality as the popular state management frameworks and not being a framework itself. Let’s look at this approach more closely.

Better readability & less boilerplate

We’ve all had these moments

Readability is one of the first things I pay attention to as I choose a tool I am going to be working with. For me, the main criterion upon which I evaluate the quality of my own code is how well am I able to understand it a week after and a month after it was written.

Of course, the tool you are using isn’t always as impacting on your code readability and self-documentability as much as your own set of habits and choices, but it still may be a contributing factor nonetheless. In my experience, the more verbosity and indirection is associated with creating a specific functionality, the less you will be able to understand it later on.

And this is true of Redux. Before even getting to the core of what it is you are doing, you have to write your actions (some people write action creators on top of that), create reducers, compose them, connect dispatch and state to props, and only then introduce the logic, and don’t forget you have to write sagas or thunks for side effects.

When you are trying to understand the structure of a Redux application, you are facing similar challenges, you have to understand what actions trigger what side effects, what happens in reducers and how it all fits together. I’ve always considered it being a nightmare, and, despite my many attempts, never really understood my colleagues who were very passionate about Redux. I would say Redux and readability are essentially the opposites. Feel free to disagree, but please make an educated argument in the comments for me and other people to be able to learn.

MobX significantly improves the readability aspect by not having the verbosity and indirection of Redux. Yet it can be confusing to mentally derive the structure of the state that consists of composed stores, as they can become a mess of properties and update logic. So how does it compare to the RxJS Hook?

Since you’re essentially using RxJS Hook as the familiar useState hook, it should be very easy to read and understand. Your state lives inside of a Subject that you can always retrieve later and analyze as you are trying to understand what the application is doing.

Close to React

It’s quite an atomic family

React is elegant. The state managers are not. Period. If there is an opportunity to stay with React before introducing other frameworks, I will use that opportunity. I am convinced that you should be able to compose complex and elegant application code with essentially React and Services logic while not sacrificing readability, maintainability, and scalability.

RxJS and React Hooks are actually the combination I find extremely helpful in this regard. Having actually written production code with this, I can tell you from my experience that it was and still is a great ride.

State Management Functionality

Creation of the modern front-end universe

I never really understood the popularity of Redux. Don’t get me wrong, I always thought it was pretty interesting, but I never liked writing my code using it. I wonder if its popularity really comes from the fact that people like Dan Abramov, who is a great guy and a great engineer, but it doesn’t make sense why it would still worth biting the bullet.

The main merit of Redux for me is in its three principles of course, which are:

  • Single source of truth
  • State is read-only
  • Changes are made by pure functions

So none of these principles actually work for MobX! With MobX you don’t have a centralized state object. You have a centralized store object. Know the difference.

And since you don’t have a centralized state object, you can’t use it to easily reason about your application as your stores and your state are interleaved and can be pretty hard to unravel.

With RxJS Subjects and React Hooks we get the one principle out of the box – the pure-functions, as observable streams are themselves pure-functions. Unfortunately, the state of a BehaviorSubject is not read-only, and you can manually change it after you get it with getValue(). I will try to ask the community if it is possible to make it read-only out of the box so that you can’t shoot yourself in the foot with this, as I am pretty sure it could just be something the contributors have overlooked. But as to the single source of truth, it is actually not that difficult to introduce by composing all the subjects into one observable, similar to how you compose reducers together.

Single Source of Truth

const subject001 = new BehaviorSubject({});
const subject002 = new BehaviorSubject({});
const subject003 = new BehaviorSubject({});

// Only for listening
const globalObservable = combineLatest(subject001, subject002, subject003);

In general, the RxJS Hook approach is not that different from Flux. You have:

  • Dispatcher/action – / setSharedState()
  • Store – globalObservable
  • View – well, your view


So here is your shared state!

const subject = new BehaviorSubject({ message: ‘’ });

const AwesomeComponent = () => {
 const [{ message }, setState] = useSharedState(subject); // Custom Hook
 return <div>{message}</div>;

Nothing unusual: we are getting a tuple of value and setState function.

Do you want to use it like useState?


const AwesomeComponent = () => {
 const [{ message }, setState] = useSharedState(subject);
 return <div
   onClick={() => setState({ message: ‘test’ })} // One way
   onClick={() =>{ message: ‘test’ })} // Another way
}; is another way to update the logic, but I think setState from the tuple makes it more readable and easy to understand.

Do you want it to be shared between components at the top of the tree as well as the bottom?


const First = () => {
 const [{ message }, setState] = useSharedState(subject);
 return <div>First: {message}</div>
const Second = () => <div><First/></div>
const Third = () => <div><Second/></div>
const Fourth = () => <div><Third/></div>

const Fifth = () => {
 const [{ message }, setState] = useSharedState(subject);
 return <div><div>Fifth: {message}<div><Fourth/></div>

Notice that we can use the same state easily, where in the past you would have to pass everything through props.

Do you want to use both local and shared states?


// Single shared and local states
const AwesomeComponent = () => {
 const [sharedState, setSharedState] = useSharedState(subject);
 const [state, setState] = useState(‘’);
 return <div>{/* That was easy */}</div>;

// Multiple shared and local states
const AwesomeComponent = () => {
 const [sharedState001, setSharedState001] = useSharedState(subject001);
 const [sharedState002, setSharedState002] = useSharedState(subject002);
 const [sharedState003, setSharedState003] = useSharedState(subject003);
 const [state001, setState001] = useState(‘test’);
 const [state002, setState002] = useState(‘test’);
 const [state003, setState003] = useState(‘test’);
 return <div>{/* That was easy */}</div>;

Another great thing is that you actually can have multiple little states as part of your component state.

Oh, but what if I want to update this state outside the component…

Easy!{ message: ‘I know Kung Fu’ });

Or so the course certificate says

You can update state from anywhere in the application, by just using It is extremely useful! MobX and Redux could never give you this much freedom.

Still not convinced?

I rewrote the Redux tutorial using the RxJS Hooks approach. Feel free to compare two codebases that do the same thing.

Side Notes

I think somebody brought to my attention that using components with subjects makes them tightly coupled. It is true in a sense where, for example, you are importing something from an external library which makes a hard dependency. But having hard dependencies doesn’t mean you can’t make whatever it is you are building highly reusable. A styled component inside of my custom component is a hard dependency, but it is abstracted out when I import my custom component. The same goes for this approach.


Behind the custom hook is a little pretty eight-liner:

const useSharedState = subject => {
 const [value, setState] = useState(subject.getValue());
 useEffect(() => {
   const sub = subject.pipe(skip(1)).subscribe(s => setState(s));
   return () => sub.unsubscribe();
 const newSetState = state =>;
 return [value, newSetState];

Just an ordinary day of a React developer

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about React

React - The Complete Guide (incl Hooks, React Router, Redux)

Modern React with Redux [2019 Update]

Best 50 React Interview Questions for Frontend Developers in 2019

JavaScript Basics Before You Learn React

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Reactjs vs. Angularjs — Which Is Best For Web Development

React + TypeScript : Why and How

How To Write Better Code in React

React Router: Add the Power of Navigation

Getting started with React Router

Using React Router for optimizing React apps

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Building REST API with Nodejs / MongoDB /Passport /JWT

#reactjs #javascript

What is GEEK

Buddha Community

React Hooks + RxJS Subjects
Autumn  Blick

Autumn Blick


How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

What are hooks in React JS? - INFO AT ONE

In this article, you will learn what are hooks in React JS? and when to use react hooks? React JS is developed by Facebook in the year 2013. There are many students and the new developers who have confusion between react and hooks in react. Well, it is not different, react is a programming language and hooks is a function which is used in react programming language.
Read More:-

#react #hooks in react #react hooks example #react js projects for beginners #what are hooks in react js? #when to use react hooks

Hayden Slater


Validating React Forms With React-Hook-Form

Validating inputs is very often required. For example, when you want to make sure two passwords inputs are the same, an email input should in fact be an email or that the input is not too long. This is can be easily done using React Hook From. In this article, I will show you how.

Required Fields

The most simple, yet very common, validation is to make sure that an input component contains input from the user. React Hook Form basic concept is to register input tags to the form by passing register() to the tag’s ref attribute. As we can see here:

#react-native #react #react-hook-form #react-hook

The Ugly Side of React Hooks

In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.

Let’s break down the motivation for ditching classes in favor of hooks, as described in the official React’s docs.

Motivation #1: Classes are confusing

we’ve found that classes can be a large barrier to learning React. You have to understand how "this"_ works in JavaScript, which is very different from how it works in most languages. You have to remember to bind the event handlers. Without unstable syntax proposals, the code is very verbose […] The distinction between function and class components in React and when to use each one leads to disagreements even between experienced React developers._

Ok, I can agree that

thiscould be a bit confusing when you are just starting your way in Javascript, but arrow functions solve the confusion, and calling a_stage 3_feature that is already being supported out of the box by Typescript, an “unstable syntax proposal”, is just pure demagoguery. React team is referring to theclass fieldsyntax, a syntax that is already being vastly used and will probably soon be officially supported

class Foo extends React.Component {
  onPress = () => {

  render() {
    return <Button onPress={this.onPress} />

As you can see, by using a class field arrow function, you don’t need to bind anything in the constructor, and

this will always point to the correct context.

And if classes are confusing, what can we say about the new hooked functions? A hooked function is not a regular function, because it has state, it has a weird looking

this(aka_useRef_), and it can have multiple instances. But it is definitely not a class, it is something in between, and from now on I will refer to it as aFunclass. So, are those Funclasses going to be easier for human and machines? I am not sure about machines, but I really don’t think that Funclasses are conceptually easier to understand than classes. Classes are a well known and thought out concept, and every developer is familiar with the concept ofthis, even if in javascript it’s a bit different. Funclasses on the other hand, are a new concept, and a pretty weird one. They feel much more magical, and they rely too much on conventions instead of a strict syntax. You have to follow somestrict and weird rules, you need to be careful of where you put your code, and there are many pitfalls. Telling me to avoid putting a hook inside anifstatement, because the internal mechanism of hooks is based on call order, is just insane! I would expect something like this from a half baked POC library, not from a well known library like React. Be also prepared for some awful naming like useRef (a fancy name forthis),useEffect ,useMemo,useImperativeHandle(say whatt??) and more.

The syntax of classes was specifically invented in order to deal with the concept of multiple instances and the concept of an instance scope (the exact purpose of

this ). Funclasses are just a weird way of achieving the same goal, using the wrong puzzle pieces. Many people are confusing Funclasses with functional programming, but Funclasses are actually just classes in disguise. A class is a concept, not a syntax.

Oh, and about the last note:

The distinction between function and class components in React and when to use each one leads to disagreements even between experienced React developers

Until now, the distinction was pretty clear- if you needed a state or lifecycle methods, you used a class, otherwise it doesn’t really matter if you used a function or class. Personally, I liked the idea that when I stumbled upon a function component, I could immediately know that this is a “dumb component” without a state. Sadly, with the introduction of Funclasses, this is not the situation anymore.

#react #react-hooks #javascript #reactjs #react-native #react-hook #rethinking-programming #hackernoon-top-story

Juana  O'Keefe

Juana O'Keefe


Hooks, Hooks, Hooks!

Prior to 2018, React, an already powerful and widely-used javascript library for building user interfaces, had 3 cumbersome issues:

  1. Reusing logic: in order to to create dynamic interfaces where state is manipulated, logic was constantly being copied for seemingly simple tasks like updating the state from a form field. This often would lead to complicated and bloated data structures.
  2. Giant components: logic often times gets split amongst various lifecycle. methods in order to keep your application working.
  3. Confusing classes: invariably with reused logic and oversized components, our classes themselves can become confusing for both user and the machine.

As Dan Abramov of the React Dev team describes it, these are really three separate issues, but rather systems of the same problem: before 2018, _React did not provide a stateful primitive that is simpler than incorporating a class component and its associated logic. _At one point, React used mixins to pseudo-resolve this issue, but that ultimately created more problems that it solved.

How did the React team resolve this seemingly singular, but hugely impactful inconvenience? Hooks to the rescue.

Image for post

#software-engineering #react-conf-2018 #hooks #react #react-conference #react native