Reputation: 111
I am trying to access and set the state of {name}, in the query URL so it can be updated with a search bar component. How do I access the state of {name}?
api
export const weatherApi = createApi({
reducerPath: 'weatherApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://api.openweathermap.org/data/2.5/' }),
endpoints: (builder) => ({
getCityByName: builder.query({
query: (name) => `weather?q=${name}&appid=${apiKey}`,
}),
}),
})
// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const { useGetCityByName } = weatherApi;
would like to access and update state of {name} is this component
import React from 'react';
import { useState } from 'react';
import { weatherApi } from '../../api/apiSlice';
export default function Forecast() {
const{ data, error, isLoading, isSuccess } = weatherApi.endpoints.getCityByName.useQuery('Dallas')
console.log(weatherApi.endpoints.getCityByName)
console.log(data);
const kelvinToFarenheit = (k) => {
return 1.8 * (k - 273.15) + 32;
};
return (
<div>
<h3>{data?.name}</h3>
<article>Temp: {Math.floor(kelvinToFarenheit(data?.main.temp))}</article>
<article>Feels Like: {Math.floor(kelvinToFarenheit(data?.main.feels_like))}</article>
</div>
)
}
Upvotes: 1
Views: 235
Reputation: 111
I've been looking at this for days and just after posting I figure it out...irony
const [cityName, setCityName] = useState({name:"Dallas"})
const { data, error, isLoading, isSuccess } = weatherApi.endpoints.getCityByName.useQuery(cityName.name)
Upvotes: 1