From the course: React with Styled Components

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Working with dynamic data

Working with dynamic data - React.js Tutorial

From the course: React with Styled Components

Working with dynamic data

- [Instructor] Let's dig a bit deeper into working with dynamic data now. One common scenario is fetching data from an external source, such as an API. We already covered this a little bit quickly in chapter two. So now let's enhance our ProductDetails example to fetch dynamic data. So in this example, we're going to use the useState and useEffects hooks to fetch dynamic data from an API when the component mounts. So first I will import useEffect and useState from my React library. And then I'll start by declaring the starting state of an array of products. So this will be used instead of this product object that we've already declared here. So we'll make const and then declare the state of this array, so, [Product, setProduct] = useState and we'll set the initial state to null so there's nothing here in this array. Then using the React useEffect hook, we can fetch data from an API. The useEffect hook allows you to…

Contents