Giter VIP home page Giter VIP logo

Comments (4)

alex-cory avatar alex-cory commented on May 14, 2024 1

Will take a look asap

from use-http.

alex-cory avatar alex-cory commented on May 14, 2024

Okay, so we don't typically use onUpdate for something like this. Having onUpdate here would make every http request on your page re-run if the token changed which does not sound like something you want.

We would do it something like

const CustomProvider = ({ children }) => {
  const [token, setToken] = useLocalStorage('token')

  const getToken = async () => {
    return 'my-token'
  }

  const options = {
    interceptors: {
      request: async options => {
        if (!token) {
          const newToken = await getToken()
          setToken(newToken)
        }
        console.log('Interceptor called with token', token)
        options.headers['x-token'] = token
        return options
      },
    },
  }

  useEffect(() => {
    console.log('Logging token', token)
  }, [token])

  return (
    <Provider url={window.location.origin} options={options}>
      {children}
    </Provider>
  )
}

const TestFetch = () => {
  const [token] = useLocalStorage('token')

  const [request, response] = useFetch()

  useEffect(() => {
    async function getSomething() {
      console.log('Performing request with token in context as:', token)
      await request.get('/test.json')
      if (response.ok) console.log('Finished.')
    }
    getSomething()
  }, [])

  return (
    <>
      <span>Token is {token}</span>
    </>
  )
}

const App = () => {
  return (
    <TestProvider>
      <CustomProvider>
        <Snowflakes>
          <Center>
            <TestFetch />
          </Center>
        </Snowflakes>
      </CustomProvider>
    </TestProvider>
  )
}

Does this help at all?

from use-http.

revolucion avatar revolucion commented on May 14, 2024

Thanks for getting back at me!

The onUpdate was something I tried with the idea of "lets see if this works". So that shouldn't have been in the example :)
I understand the example that you're giving, however something still seems to not be working.
I have updated the example with some clearer code perhaps.

You can see in the console how the CustomProvider logs the state object as it should be when the state changes, however the request is still performed without the updated state values.

Updated example here

Perhaps I'm missing something obvious.

from use-http.

alex-cory avatar alex-cory commented on May 14, 2024

Apologies for the delay on this, but I think this PR might fix this. Try v0.2.3. I'm going to close this. If the issue persists, let me know and I will reopen.

from use-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.