Skip to content

useHttp React example: setData followed by get() sends stale data #60

@gordanielyan

Description

@gordanielyan

The React example in the HTTP Requests documentation shows:

function search(e) {
    setData('query', e.target.value)
    get('/api/search', {
        onSuccess: (response) => {
            console.log(response)
        },
    })
}

Since setData is a React state setter, calling get() immediately after sends the previous state value rather than the updated one. This causes the search query to always be one character behind what the user typed.

Steps to reproduce:

Use the useHttp hook as shown in the docs
Type "abc" into the input
Observe that the first request sends "", the second sends "a", the third sends "ab"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions