React Archives - Thoughts https://blog.jstassen.com/tag/react/ A place for collecting my miscellaneous thoughts. Sun, 25 Feb 2024 10:26:07 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.7 92836340 When do you need useMemo (and when don’t you) https://blog.jstassen.com/2024/02/when-do-you-need-usememo-and-when-dont-you/ https://blog.jstassen.com/2024/02/when-do-you-need-usememo-and-when-dont-you/#respond Sun, 25 Feb 2024 10:26:03 +0000 https://blog.jstassen.com/?p=1437 It’s confusing when we should use useMemo and when we shouldn’t. useMemo isn’t free $ useMemo comes with a small cost. So we should understand when it’s a benefit, and when it doesn’t provide any value. So we should be smart when we use it. useMemo isn’t always needed useMemo isn’t needed in certain cases. For example, … Continue reading When do you need useMemo (and when don’t you) →

The post When do you need useMemo (and when don’t you) appeared first on Thoughts.

]]>
It’s confusing when we should use useMemo and when we shouldn’t.

useMemo isn’t free $

useMemo comes with a small cost. So we should understand when it’s a benefit, and when it doesn’t provide any value.

  • Small usage in RAM to memoize & remember the resulting value
  • It costs a small cpu cycle to loop over & compare the dependency array & run internal fn of useMemo

So we should be smart when we use it.

useMemo isn’t always needed

useMemo isn’t needed in certain cases. For example, casting something to Boolean, or doing simple math isn’t an expensive operation and returns a primitive like “boolean” or “number”. These value will always be the same every time they are re-render run 

true === true and 5 === 5.

Conversely, an array or object don’t have equality,

 [] !== [] and {} !== {} and new Date !== new Date.

Two tests of if you need useMemo

  1. Is the calculation to get the value is complex (looping, calculating, initializing a class)?
    • Or is it cheap (comparisons of numbers, boolean, casting types, simple math)
  2. Is the returning complex value (object, array, fn, or class)?
    • Or is it a primitive value that is simple to compare (number, boolean, string, null, undefined)

Examples

  const date = useMemo(() => Date.parse(isoDateString), [isoDateString]);

We should use useMemo

  1. 🛑 Initializes the Date class
  2. 🛑 Returns a Date Object, which is not a primitive

  const isAdmin = useMemo(() => runExpensiveSearch(accounts), [accounts]

We should use useMemo

  1. 🛑 Runs an expensive function to get the value
  2. ✅ Returns a primitive value

In cases where it’s neither an expensive calculation nor a complex object, useMemo isn’t necessary.


-  const isArchived = useMemo(() => Boolean(process?.deletedAt), [process?.deletedAt]);
+  const isArchived = Boolean(process?.deletedAt);

We don’t need useMemo

  1. ✅ Casts to a boolean which is a cheap operation
  2. ✅ Returns a primitive value

-  const numberOfAccounts = useMemo(() => accounts.length, [accounts]);
+  const numberOfAccounts = accounts.length;

We don’t need useMemo

  1. ✅ Getting the length property is cheap
  2. ✅ Returns a primitive value

Just remember the two tests

  • Is it complex / expensive function?
  • Is the value not a primitive?

What about useCallback?!

I’m so glad you asked! The principles are similar. In general, all callback functions should be memoized via useCallback. Functions are considered a non-primitive value thus never have equality unless we memoize them via useCallback.

(Cover Photo: Feldstraße Bunker, Hamburg, Germany – Jonathan Stassen / JStassen Photography)

The post When do you need useMemo (and when don’t you) appeared first on Thoughts.

]]>
https://blog.jstassen.com/2024/02/when-do-you-need-usememo-and-when-dont-you/feed/ 0 1437
setup VSCode with REACT_EDITOR (or any editor) https://blog.jstassen.com/2020/10/setup-vscode-with-react_editor-or-any-editor/ https://blog.jstassen.com/2020/10/setup-vscode-with-react_editor-or-any-editor/#respond Wed, 07 Oct 2020 19:59:19 +0000 http://blog.jstassen.com/?p=1188 When an error occurs with an app Created with Create React App the error screen is super helpful. You can click on this error in the browser to jump directly to the line of code inside your editor! But how do you setup VSCode with REACT_EDITOR? Console error: Could not open SomeFile.js in the editor.To … Continue reading setup VSCode with REACT_EDITOR (or any editor) →

The post setup VSCode with REACT_EDITOR (or any editor) appeared first on Thoughts.

]]>
When an error occurs with an app Created with Create React App the error screen is super helpful.

You can click on this error in the browser to jump directly to the line of code inside your editor! But how do you setup VSCode with REACT_EDITOR?

Console error:

Could not open SomeFile.js in the editor.
To set up the editor integration, add something like REACT_EDITOR=atom to the .env.local file in your project folder and restart the development server. Learn more: https://goo.gl/MMTaZt

Setup your editor to be open from terminal

Google “How to open [your editor] from terminal” to find instructions for your editor.

For VSCode, open the command pallet ( Command Key + P ) and type > shell to install “code” into your PATH environment variables.

You should now be able to open terminal and type code to open VSCode. Pretty nifty!

Setup your React Project to know which editor to open

Create a new file (or edit the existing) .env.local file.

setup VSCode with REACT_EDITOR this will be REACT_EDITOR=code and for other editors set it to the command you found to open your editor from the terminal.

Credit to this StackOverflow Answer.

Related post: Favorite Sublime 3 Packages

(cover photo credit: Jonathan Stassen / JStassen Photography)

The post setup VSCode with REACT_EDITOR (or any editor) appeared first on Thoughts.

]]>
https://blog.jstassen.com/2020/10/setup-vscode-with-react_editor-or-any-editor/feed/ 0 1188
An Introduction to Redux https://blog.jstassen.com/2020/07/an-introduction-to-redux/ https://blog.jstassen.com/2020/07/an-introduction-to-redux/#respond Tue, 07 Jul 2020 06:41:00 +0000 http://blog.jstassen.com/?p=1075 Redux’s ideology is a unidirectional data flow. This pattern reduces long term complexity, encourages re-usability, and generally discourages spaghetti code. 🍝 Video Series: https://egghead.io/courses/getting-started-with-redux Terms & Descriptions The core of everything is the Store, Action, and Dispatch. In its simplest form it’s all you technically need. From there Thunks & Sagas enhance the the tooling … Continue reading An Introduction to Redux →

The post An Introduction to Redux appeared first on Thoughts.

]]>
Redux’s ideology is a unidirectional data flow. This pattern reduces long term complexity, encourages re-usability, and generally discourages spaghetti code. 🍝

Video Series: https://egghead.io/courses/getting-started-with-redux

Terms & Descriptions

The core of everything is the Store, Action, and Dispatch. In its simplest form it’s all you technically need. From there Thunks & Sagas enhance the the tooling around the Dispatching of Actions.

Store

  • A singleton object/state for the app.

Action

Dispatch

  • You “Dispatch an Action to the Store” to update the store.
  • Dispatch sends the Action payload through the Reducer.

Reducer

  • Receives the Action payload from Dispatch and modifies the Store.
  • Reducers contain no business logic, they only modify the Store described in the Action

Thunk

  • Considered the old way, but sometimes still has great applications in simple cases.
  • You can “Dispatch a Thunk to either access state or do ajax work”
  • Within a thunk you can call additional Dispatch
  • Within a thunk you can access the state of the store.
    • Good for conditionally firing subsequent api calls, or dispatches.
    • Good for pulling together data from the store into a dispatch.
  • Good for very simple ajax calls, you can Dispatch Actions from ajax response
  • Best way to understand Thunks in my opinion is to look at the 10 lines of source code:

Saga

  • Regarded as a better replacement for Thunks
  • Can require more effort than Thunks to understand, and build.
  • Within a Saga you can access the state of the store.
    • Great for conditionally firing subsequent api calls, or dispatches.
    • Great for pulling together data from the store into a dispatch.
  • Sagas can subscribe to listen and fire when some Actions have been Dispatched
  • Great for moving side effects to be self contained instead of sprinkled throughout the app
  • Provides great debounce / cancel / takelatest logic nearly for free.
  • Can do long running / scheduled Actions
  • https://redux-saga.js.org/docs/introduction/BeginnerTutorial.html

(cover photo credit: Jonathan Stassen / JStassen Photography)

The post An Introduction to Redux appeared first on Thoughts.

]]>
https://blog.jstassen.com/2020/07/an-introduction-to-redux/feed/ 0 1075
React “stateProps PrecalculationError” and TypeError ‘state’ of undefined https://blog.jstassen.com/2016/07/react-statepropsprecalculationerror/ https://blog.jstassen.com/2016/07/react-statepropsprecalculationerror/#respond Tue, 26 Jul 2016 23:29:52 +0000 http://blog.jstassen.com/?p=750 Debugging a React component can be a pain. There are many causes for “statePropsPrecalculationError”, for debugging see my post on Debugging React statePropsPrecalculationError post. When you specifically have ” ‘state’ of undefined” the fix is simple: Recently I ran into “statePropsPrecalculationError” alongside with the the console warning TypeError: Cannot read property ‘state’ of undefined(…)`. This … Continue reading React “stateProps PrecalculationError” and TypeError ‘state’ of undefined →

The post React “stateProps PrecalculationError” and TypeError ‘state’ of undefined appeared first on Thoughts.

]]>
Debugging a React component can be a pain.

There are many causes for “statePropsPrecalculationError”, for debugging see my post on Debugging React statePropsPrecalculationError post.

When you specifically have ” ‘state’ of undefined” the fix is simple:

Recently I ran into “statePropsPrecalculationError” alongside with the the console warning TypeError: Cannot read property ‘state’ of undefined(…)`.

This is super easy to fix, but not immediately obvious. Whenever you use `this.setState()` inside your component, you must define your initial state!

Just add a `getInitialState()` definition!


const MyComponent = React.createClass({
getInitialState() {
return {
isOpen: false
};
},
toggleOpen() {
this.setState({isOpen: !this.state.isOpen});
},
render() {
return (

Hello World!

);
}
});

The post React “stateProps PrecalculationError” and TypeError ‘state’ of undefined appeared first on Thoughts.

]]>
https://blog.jstassen.com/2016/07/react-statepropsprecalculationerror/feed/ 0 750