score:2
Accepted answer
Your hook can provide a function to fetch/refetch the data:
const useFetch = (url, options) => {
const [response, setResponse] = React.useState(null);
const [error, setError] = React.useState(null);
const [isLoading, setIsLoading] = React.useState(false);
const fetchData = async (resortId) => {
setIsLoading(true);
try {
const res = await fetch(url, {id: resortId});
const json = await res.json();
setResponse(json);
setIsLoading(false);
} catch (error) {
setError(error);
setIsLoading(false);
}
}
return [
response,
error,
isLoading,
fetchData
];
};
You can then call fetchData whenever you want to update your search:
const App = () => {
const [resort, resortIsLoading, resortError, fetchResort] = useFetch(config.API_URL_GET_RESORT);
const handleChangeResort = (event) => {
const id = event.target.value;
fetchResort(id);
};
return (
<>
<Select
labelId="resort-label"
id="resort"
label="resort"
value={resort ? resort.id : ""}
onChange={handleChangeResort}
>
{resorts.length ? (
resorts.map(c => (
<MenuItem key={c.id} value={c.id}>
{c.name}
</MenuItem>
))
) : (
<MenuItem key={0} value={""}>
<em>loading resorts list...</em>
</MenuItem>
)}
</Select>
</>
)}
}
score:1
I would do something like so:
const useFetch = (url) => {
const [state, setState] = React.useState([null, false, null]);
const fetchData = async (options) => {
setState([null, true, null]);
try {
const res = await fetch(url, options);
const json = await res.json();
setState([json, false, null]);
} catch (error) {
setState([null, false, error]);
}
}
return [
...state, // response, loading, error
fetchData
];
};
And then, you could use:
const [resorts, resortsIsLoading, resortsError, fetchResorts] = useFetch(config.API_URL_GET_RESORTS);
const [resort, resortIsLoading, resortError, fetchResort] = useFetch(config.API_URL_GET_RESORT);
useEffect(() => {
fetchResorts()
}, [])
Source: stackoverflow.com
Related Query
- Reactjs: how to call useFetch after a variable is set
- how to call a function after completion of setting a state variable of a functional component in reactjs
- how to set url in another input after upload file in reactjs
- How to set a local variable without using useState in ReactJs
- Not able to set the state in the ReactJS context API after AJAX call
- how to call a function after completion of setting of group of state variables of functional component in reactjs
- How to use an async database call to set a variable with useState() and useEffect()?
- How to set SCSS variable value programatically in ReactJs
- how to call action after every 2 minute in reactjs with await
- How can I call a function that set sets an outside variable inside a custom component in React Native?
- How to set environment variable for a deployed Reactjs app on server?
- How to set a method inside one onClick method In ReactJS to finish execution after API call?
- how to set original state after category filter for lowest price to all in reactjs
- useeffect() run and set data to the variable after the return() run in react? how to handle this?
- ReactJS How to call a function to change JSON dict value after setState?
- How to set focus on an input field after rendering?
- How to make a rest post call from ReactJS code?
- reactjs - how to set inline style of backgroundcolor?
- ReactJS - How can I set a value for textfield from material-ui?
- How do I call setState from another Component in ReactJs
- How can I call methods on Reactjs components in the browser console or from an outside script?
- How to set environment variable in React JS..?
- How can I ensure a reactjs state is updated, and then call a function?
- How to call multiple functions on the same onClick event ReactJS
- How to set variable in render with reactjs?
- How to pass in a second argument on ReactJS onSubmit function call
- How to redirect after success from ajax call using React-router-component?
- How to set the Google Analytics cookie only after another consent cookie is set and "true"?
- How to make React input onChange set state only after onChange stops firing for set time?
- Typescript & ReactJS How To Dynamically Set State
More Query from same tag
- How to remove an object item from an object where each value is an array of objects?
- Single responsibility principle React refactor
- Render React page dynamically using URL parameter
- How to get a component based on a string value?
- How to group elements inside <g> tag in joint.js?
- How can I display/hide a component depending on a property on the Redux store?
- React: Make flash message disappear automatically
- ReactJS: add data to document when creating a subcollection
- How to refresh grid after row delete in react ag grid
- Can I have a ReactClass inside a ReactClass?
- React Redux: redirect user (history.push()) when certain http status codes are received during async action (thunk)
- Problem with react-signature-canvas (offset ink)
- Re-render child when prop changes in App.js
- How to simulate events using React-Redux?
- React: How to inherit PropTypes?
- React Material UI Slider to change Border Radius
- In Promise, response.status returns other Promise rather than the status code
- They don't load the elements inside the div that glider-js creates in nextjs
- Call React hook depending on Promise
- how to implement a progress bar in react?
- Why input data not getting added to firebase firestore collection
- ReactJS: 'type is undefined' when loading a component
- Is there a way for a React Component to be notified when the element it is attached to is deleted outside of React
- HashRouter tips error in react router 4.x
- Multiple `setState` not synchronized - if one uses a value obtained from `await`
- Update React Data when MySQL Data Changes
- is there any easy var to handle button state in react?
- How to find the DOM element of a functional children in React?
- implementation of react-input-mask
- React: How to load components properly?