Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

fetch await reactjs

const [products, setProducts] = useState([]);

// Fetch array of products
async function initProducts() {
    await fetch('http://127.0.0.1:5000/listProducts')
        .then(response => response.json())
        .then(response => {
            setProducts(response.result);
            console.log(response.result);
        )
        .catch(err => console.error(err));
}

// Get products to html with map function
function getProductsHtml() {
    return products.map(product =>
        <h1>{product.ProductName}</h1>
        <h1>{product.ProductDescription}</h1>
    );
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #fetch #await #reactjs
ADD COMMENT
Topic
Name
9+5 =