How to send http request on button click with react hooks? Or, for that matter, how to do any side effect on button click?
What i see so far is to have something "indirect" like:
export default = () => { const [sendRequest, setSendRequest] = useState(false); useEffect(() => { if(sendRequest){ //send the request setSendRequest(false); } }, [sendRequest]); return ( <input type="button" disabled={sendRequest} onClick={() => setSendRequest(true)} ); }
Is that the proper way or is there some other pattern?
Example: Pass a Button Value to an Inline Function Notice the value e that's returned from the onClick event handler: import React from 'react'; function App() { return ( <button value="hello!" onClick={e => alert(e. target. value)}> Click me!
Syntax: const authContext = useContext(initialValue); The useContext accepts the value provided by React. createContext and then re-render the component whenever its value changes but you can still optimize its performance by using memoization.
Use the useEffect hook to only call a function once in React. When the useEffect hook is passed an empty dependencies array, it is only ran when the component mounts. This is the preferred approach when you have to fetch data when the component mounts.
export default () => { const [isSending, setIsSending] = useState(false) const sendRequest = useCallback(async () => { // don't send again while we are sending if (isSending) return // update state setIsSending(true) // send the actual request await API.sendRequest() // once the request is sent, update state again setIsSending(false) }, [isSending]) // update the callback if the state changes return ( <input type="button" disabled={isSending} onClick={sendRequest} /> ) }
this is what it would boil down to when you want to send a request on click and disabling the button while it is sending
@tkd_aj pointed out that this might give a warning: "Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function."
Effectively, what happens is that the request is still processing, while in the meantime your component unmounts. It then tries to setIsSending
(a setState) on an unmounted component.
export default () => { const [isSending, setIsSending] = useState(false) const isMounted = useRef(true) // set isMounted to false when we unmount the component useEffect(() => { return () => { isMounted.current = false } }, []) const sendRequest = useCallback(async () => { // don't send again while we are sending if (isSending) return // update state setIsSending(true) // send the actual request await API.sendRequest() // once the request is sent, update state again if (isMounted.current) // only update if we are still mounted setIsSending(false) }, [isSending]) // update the callback if the state changes return ( <input type="button" disabled={isSending} onClick={sendRequest} /> ) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With