question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposed Improvements

See original GitHub issue

Current Code

export default function useForceUpdate(): () => void {
  const [ , dispatch ] = useState<{}>(Object.create(null));

  // Turn dispatch(required_parameter) into dispatch().
  const memoizedDispatch = useCallback(
    (): void => {
      dispatch(Object.create(null));
    },
    [ dispatch ],
  );
  return memoizedDispatch;
}

Proposed Improvements

JS, but can easily be typed:

import { useRef, useState } from 'react';

// Creates an empty object, but one that doesn't inherent from Object.prototype
const newValue = () => Object.create(null);

export default () => {
  const setState = useState(newValue())[1];

  const forceUpdate = useRef(() => {
    setState(newValue());
  }).current;

  return forceUpdate;
};

Rational:

First, dispatch unclear. Looking at the history, it is a residue from when useReducer was used, but this is no longer the case.

Then, from the docs (see note):

React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list.

So there’s no need providing it as a useCallback dependency.

Either useCallback or useMemo with empty dependencies ([] - ie, one-off) is identical to useRef().current which somehow communicates the intent better - this value will not change. Also, with useRef, react doesn’t call Object.is(old, new) each render.

Also note that in the current code we have memoizedDispatch but because dispatch is stable, there’s no real memoization going on - useCallback will always return the same function.


Finally, in my case, initial render could trigger dozens of forceUpdates so a further improvement could be (I’m not actually using it because the trade-off doesn’t seem to be worth it, but others may find this useful):

import { useRef, useState, useEffect } from 'react';

// Creates an empty object, but one that doesn't inherent from Object.prototype
const newValue = () => Object.create(null);

export default () => {
  const setState = useState(newValue())[1];

  const updatePending = useRef(false);

  const forceUpdate = useRef(() => {
    if (!updatePending.current) {
      setState(newValue());
      updatePending.current = true;
    }
  }).current;

  useEffect(() => {
    updatePending.current = false;
  });

  return forceUpdate;
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
CharlesStovercommented, Jan 3, 2020

@evoyy Symbol has poor browser support. As much as I dislike IE, I wouldn’t want to cut out its users from all dependents on this package.

1reaction
CharlesStovercommented, Nov 15, 2019

To be honest, I just came across the React implementation for this, which I reckon is superior to the proposed above:

This was the original implementation in this package, and it was requested to be changed so as not to have to deal with max integer values in [primarily older] browsers with a cap.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposed Improvement Definition | Law Insider
Examples of Proposed Improvement in a sentence. If Lessee does not complete each and every Proposed Improvement within following the Commencement Date and ......
Read more >
proposed improvement collocation | meanings and examples ...
The proposed improvement includes extensive environmental protection: 6 to 9 ft noise barriers arid bunding are proposed at sensitive locations.
Read more >
Proposed GAAP Taxonomy Improvements - FASB
Proposed GAAP Taxonomy improvements related to topical reviews or other improvement projects are expected to have a 30 to 60-day comment period. These...
Read more >
Proposed Improvements to Interstate 80/35 & U.S. 6/Hickman ...
Proposed Improvements to Interstate 80/35 & U.S. 6/Hickman Road Interchange to be discussed in Urbandale on November 22, 2022.
Read more >
PROPOSED IMPROVEMENTS
PROPOSED IMPROVEMENTS · Dedicated bus lanes on select congested segments along the CityLink Blue & CityLink Orange routes to enhance bus speed and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found