Logo

dev-resources.site

for different kinds of informations.

Suggestion for new syntax of React reducers + context

Published at
3/12/2024
Categories
react
hooks
syntax
Author
francisvila
Categories
3 categories in total
react
open
hooks
open
syntax
open
Author
11 person written this
francisvila
open
Suggestion for new syntax of React reducers + context

The suggestion is this codesandbox.

I was impressed by the example suggested in Scaling Up with Reducer and Context. I spent some time understanding it, and had an idea that the advantages of this technique would justify offering a simpler way of achieving it. Seeing that the React team spectacularly simplified the syntax of function components as compared to the Component class, I hope this is in the spirit of what they are striving to achieve.

The example presented here is forked and edited from the example presented in Scaling Up with Reducer and Context. The syntax suggested here would, if implemented, produce the same effect.

Obviously, the code presented here does not currently function correctly.

The suggestion involves:

  • one new hook, named useContextReducer in the example. It combines the effect of useReducer and useContext.
  • a function to retrieve the state from the parent component, named fetchContextReducer in the example.

I see several advantages to this approach:

  • less code: the file TaskContext.js from the previous example becomes redundant.
  • code is closer to where it is used: namely in the handlers of the components that trigger them, rather than abstracted via dispatchers, type, etc.
  • syntax is similar to that of useState, making it more familiar for entry-level React developers
hooks Article's
30 articles in total
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
A Complete Guide to All React Hooks for Beginners
Favicon
Using useReducer for Complex State Logic
Favicon
Descobrindo o useCallback do React
Favicon
Discovering React’s useCallback
Favicon
Mastering Code Quality in Laravel: Pint with Git Hooks and Docker
Favicon
React: leveraging custom hooks to extract reusable logic
Favicon
Hooks Behind The Scenes 2, useState!!
Favicon
Mastering React Functional Components: Hooks, useEffect, and Lifecycle Explained
Favicon
JSHooks API compare to CHooks
Favicon
The Ultimate Guide to Wall Hanging Hooks and Display Systems for Your Home and Office
Favicon
How to Detect if an Element is in View with React
Favicon
React useQuery : A Complete Guide
Favicon
{useState} HooK { Briefly Explained};
Favicon
React HooK= { Briefly Explained};
Favicon
What is useState?
Favicon
Actions - React 19
Favicon
React controlled and uncontrolled hooks
Favicon
React Concepts: Hook Proximity
Favicon
Demystifying React's useState Hook: A Comprehensive Guide
Favicon
Mastering React Hooks: Best Practices for Efficient and Maintainable Code
Favicon
Designing React Hooks for Flexibility
Favicon
Unleashing the Power of React Custom Hooks
Favicon
Understanding Context Hooks in React: A Beginner's Guide
Favicon
3 Ways To Create Engaging React Loading Screens with Hooks
Favicon
Understanding the useInteractionTimer Hook: Measuring Component Interaction Time in React
Favicon
Cats Fatc's Quick project
Favicon
All About Custom Hooks: Supercharge Your React Components
Favicon
Suggestion for new syntax of React reducers + context
Favicon
How To Define Typescript onChange Event In React

Featured ones: