Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Axios with React Hooks, “traditional” Promise syntax

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

  useEffect(() => {
    axios
      .post(url, payload)
      .then((response) => setData(response.data))
      .catch((error) => setError(error.message))
      .finally(() => setLoaded(true));
  }, []);

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