Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Integrating Axios with React Hooks

// npm install axios
const useAxiosPost = (url, payload) => {
  const [data, setData] = useState(null);
  const [error, setError] = useState("");
  const [loaded, setLoaded] = useState(false);

  useEffect(() => {
    (async () => {
      try {
        const response = await axios.post(url, payload);

        setData(response.data);
      } catch (error) {
        setError(error.message);
      } finally {
        setLoaded(true);
      }
    })();
  }, []);

  return { data, error, loaded };
};
Source by gist.githubusercontent.com #
 
PREVIOUS NEXT
Tagged: #Integrating #Axios #React #Hooks
ADD COMMENT
Topic
Name
5+4 =