Next.js Discord

Discord Forum

Child component keeps refreshing on useState set

Answered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Avatar
Saltwater CrocodileOP
I have a parent component with an <input> onChange setSearch useState. I then have another component with params search={search}. In the child component I have useEffect to call the server with the search query. It just keeps hammering the server with constant rapid refreshes.
Answered by Pearls
It might be because you have dont have the dependencies argument in your useEffect
View full answer

18 Replies

Avatar
Saltwater CrocodileOP
export default function RepairCentreSearch() {
    const [search, setSearch] = useState('');

    return (
        <>
            <fieldset className="offset-sm-3 col-sm-6 mb-3">
                <label className="form-label mb-1" htmlFor="repairCentreSearch">Repair Centre Name</label>
                <input
                    id="repairCentreSearch"
                    className="form-control"
                    onChange={(e) => {
                        setSearch(e.target.value)
                    }}
                    placeholder="Search for your repair centre"
                />
            </fieldset>
            <div className="col-12">
                <RepairCentreResults search={search}></RepairCentreResults>
export function RepairCentreResults({search}: { search: string }) {
    const [isLoading, setIsLoading] = useState(true)
    const [repairCentres, setRepairCentres] = useState<RepairCentreDto[]>([])

    console.log(search)

    useEffect(() => {
        fetch('http://localhost:5193/GetRepairCentres?search=' + search)
            .then(res => res.json())
            .then((repairCentres : RepairCentreDto[]) => {
                setRepairCentres(repairCentres)
                setIsLoading(false)
            })
    })
    if (isLoading) return <p>Loading...</p>

    return (
        <div className="table-responsive">
Avatar
Saltwater CrocodileOP
It's actually happening on setRepairCentres
Avatar
Saltwater CrocodileOP
I'm very noobish, but I've posted four different questions over the last two months now and not one response.
Posting on Stack but I'm starting to feel pretty stupid.
Avatar
I'm on mobile so i cant get a good sense of the code but i recommend adding a button instead to fire off the setState or do it onBlur(when your input loses focus)
Avatar
It might be because you have dont have the dependencies argument in your useEffect
Answer
Avatar
https://react.dev/reference/react/useEffect

The correct syntax for a useEffect is:
useEffect(setup, dependencies?)
i would recommend to change your useEffect from this:
useEffect(() => {
        fetch('http://localhost:5193/GetRepairCentres?search=' + search)
            .then(res => res.json())
            .then((repairCentres : RepairCentreDto[]) => {
                setRepairCentres(repairCentres)
                setIsLoading(false)
            })
    })


To this:
useEffect(() => {
        fetch('http://localhost:5193/GetRepairCentres?search=' + search)
            .then(res => res.json())
            .then((repairCentres : RepairCentreDto[]) => {
                setRepairCentres(repairCentres)
                setIsLoading(false)
            })
    }, []) // <-- added the dependencies argument with 0 depencies so it doesnt refresh
If you want the api request to be executed every time 'search' changes then you might have to add the 'search' variable to the depencies list
@Saltwater Crocodile ^
Avatar
@Saltwater Crocodile If you dont need any more help, please mark a solution.
Avatar
Saltwater CrocodileOP
You are correct. It was the dependencies argument. Nice to know there's life on this planet.
Avatar
Alright, mark a solution if you dont need any more help.
Avatar
Saltwater CrocodileOP
Is there a way to mark a specific reply as the answer?
Avatar
Right click a message (or click the 3 horizontal dots) -> apps -> Mark solution