Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send request on click React Hooks way?

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?

like image 510
dee zg Avatar asked Apr 12 '19 08:04

dee zg


People also ask

How do you write onClick in React Hooks?

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!

How do you use useContext Hooks in React?

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.

How do I call API only once in React?

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.


1 Answers

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

update:

@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} />   ) } 
like image 78
DoXicK Avatar answered Sep 28 '22 15:09

DoXicK