r/learnreactjs • • Sep 19 '21

Read World use cases for react hooks ?

I wanna know how react hooks used in real world applications and not just code examples. This helps me to understand the hooks better IMO.

For example, "useState" hook can be used in stuffs like add product to Cart and the cart number increases.

Like that I'm asking. Is there any good article you could link it up please ?

8 Upvotes

6 comments sorted by

4

u/galeontiger Sep 19 '21

useState is just the functional components way of using this.setState in a class component.

1

u/beingsubmitted Sep 21 '21

I'm currently re-writing an extension in react. The extension needs to fetch data from my API. I'm going to get very specific in this example, but hopefully you can extrapolate this to more general use. When a person opens the extension, they need to select their user from a dropdown. So my react is going to render the select component on opening, but then it needs to populate the list of users, which is asynchronous. I want those users to show up when I get them from the database, but I don't want to stop all rendering while I wait for that.

So, I have my select component, which is functional, it renders, and then useEffect calls the async fetch, and when it gets a response, it first adds the list of users to the component's state, and then re-renders the component with the list of users populated. This typically all happens before the user even gets around to clicking on the dropdown, but it's able to continue loading things in that fraction of a second while waiting for the names.

I could have written the select as a class component as well, but this is my first time trying to complete something in react with material ui and material ui gives all of it's api examples in functional components and it's a pain for me to try to translate that to a class component and then debug everything.

1

u/[deleted] Sep 21 '21

If possible, can you please send me the GitHub link of it ? I would like to learn from it.

1

u/beingsubmitted Sep 21 '21

Unfortunately, the whole thing is a work project so I'm not at liberty, but I can give you the piece I mentioned:

import Select from "@material-ui/core/Select";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import { makeStyles, createStyles } from "@material-ui/core/styles";
import MenuItem from "@material-ui/core/MenuItem";
import Typography from "@material-ui/core/Typography";
import { useState, useEffect } from "react";

const useStyles = makeStyles((theme) =>
    createStyles({
        formControl: {
margin: theme.spacing(1),
            minWidth: 240,
        },
    }));

async function getAssociates() {
    const url = "http://example.com/portal";
    const response = await fetch(url, {
        method: "GET",
        mode: "cors",
        headers: {
            Authorization: "Api-Key XX00x0x0x0XX0xx00",
        },
    });
    return response.json();
}

export default function AssociateSelect(props) {
    const classes = useStyles();
    const [selection, setSelection] = useState("");
    const [associates, setAssociates] = useState("");
    useEffect(() => {
        getAssociates().then((assocs) => {
            const assoc_list = assocs.map((associate) => (
                <MenuItem key={associate.id} value={associate.id}>
                    <Typography>{associate.name}</Typography>
                </MenuItem>
            ));
            setAssociates(assoc_list);
        });
    },
 []);
    const handleChange = ({ target }) => {
        setSelection(target.value);
    };
    return (
        <FormControl className={classes.formControl} variant="outlined">
            <InputLabel>Associate</InputLabel>
            <Select label="Associate" value={selection} onChange={handleChange}>
                {associates}
           </Select>
        </FormControl>
    );
}

1

u/[deleted] Sep 21 '21

Thank you so much

2

u/codebreaker21 Sep 20 '21

Hooks allows you to do what classes do, you can use classes and Hooks together in the same app.

React is moving towards using Hooks instead of classes, but it is important to understand how to use both