DEV Community

chantastic
chantastic

Posted on

8

Connect useEffect and useState to Update Components with Data

We have one way to update (or re-render) a component.
It's React.useState.

When we want to render our component with data we've useEffected from the internet, we need to need a useState updater function to call.

This is a common pairing of functions where server data is used to update a component:

let [componentData, setComponentData] = React.useState(null);

React.useEffect(() => {
  fetchData().then(serverData => setComponentData(serverData));
})
Enter fullscreen mode Exit fullscreen mode

In our Pokemon app, that looks like this:

let [index, setIndex] = React.useState(1);
let [pokemon, setPokemon] = React.useState(null);

React.useEffect(() => {
  fetchPokemon(index).then(json => setPokemon(json));
});
Enter fullscreen mode Exit fullscreen mode

Our useEffect connects us to the outside world β€” fetching data with JavaScript.
The callback we give to fetchPokemon calls our useState updater function when data is ready β€” updating our component.

Give it a try!

Assignment Sandbox:

Assignment:

  1. Update the let pokemon assignment to get it's value from React.useState(null)
  2. Using destructuring assignment, take the second element of React.useState's return (our updater function) and setPokemon
  3. Replace console.log(json) with a call to setPokemon(json)

Follow the 🧡 on Twitter:

Subscribe on YouTube:

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (2)

Collapse
 
thorocaine profile image
Jonathan Peel β€’

How do you feel about putting {index, pokemon} into a reducer, so you only have one "state" object?

Do you think it is too much here because they always update independently?

Collapse
 
chantastic profile image
chantastic β€’

great question!

You could go either way, depending on your comfort with reducers.

I think the end result would impact my preference.
For example, I don't like having two pieces of state in a reducer that control the same outcome β€” index and pokemon.

I think it'd depend a lot of the final component whether I kept index or pokemon in the reducer.

I'll try to flesh that out a little more in the later lessons.

Thanks for asking!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay