Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

search query API example using react

function MyComponent() {
      const [error, setError] = useState(null);
      const [isLoaded, setIsLoaded] = useState(false);
      const [items, setItems] = useState([]);

      // Note: the empty deps array [] means
      // this useEffect will run once
      // similar to componentDidMount()
      useEffect(() => {
        fetch("https://api.example.com/items")
          .then(res => res.json())
          .then(
            (result) => {
              setIsLoaded(true);
              setItems(result);
            },
            // Note: it's important to handle errors here
            // instead of a catch() block so that we don't swallow
            // exceptions from actual bugs in components.
            (error) => {
              setIsLoaded(true);
              setError(error);
            }
          )
      }, [])

      if (error) {
        return <div>Error: {error.message}</div>;
      } else if (!isLoaded) {
        return <div>Loading...</div>;
      } else {
        return (
          <ul>
            {items.map(item => (
              <li key={item.id}>
                {item.name} {item.price}
              </li>
            ))}
          </ul>
        );
      }
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: form changes button enable reactive forms 
Javascript :: js string to num 
Javascript :: mongoose populate array of ids 
Javascript :: what is my version of linux mint 
Javascript :: debug bar laravel unninstall 
Javascript :: javascript reversing an array 
Javascript :: js data types 
Javascript :: mdn .map 
Javascript :: how to use .tolowercase 
Javascript :: add object to another object javascript 
Javascript :: linked list algorithm javascript 
Javascript :: object javascript 
Javascript :: update a value from array in redux state 
Javascript :: if array javascript 
Javascript :: path object d3.js 
Javascript :: pass a function as a parameter in other function 
Javascript :: javascript object/function which you want to proxy 
Javascript :: js define constant by element id 
Javascript :: take one character in end of string javascript 
Javascript :: string object javascript 
Javascript :: google js console 
Javascript :: parsley custom error message 
Javascript :: javascript stack 
Javascript :: comming soon page in react 
Javascript :: ant design form validation in the modal 
Javascript :: pass array as argument javascript 
Javascript :: javascript key value map 
Javascript :: function 
Javascript :: access css and js files inside resources folder in laravel 
Javascript :: javascript infinity 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =