Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

useeffect react example

import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";

function Reddit() {
  // Initialize state to hold the posts
  const [posts, setPosts] = useState([]);

  // effect functions can't be async, so declare the
  // async function inside the effect, then call it
  useEffect(() => {
    async function fetchData() {
      // Call fetch as usual
      const res = await fetch(
        "https://www.reddit.com/r/reactjs.json"
      );

      // Pull out the data as usual
      const json = await res.json();

      // Save the posts into state
      // (look at the Network tab to see why the path is like this)
      setPosts(json.data.children.map(c => c.data));
    }

    fetchData();
  }); // <-- we didn't pass a value. what do you think will happen?

  // Render as usual
  return (
    <ul>
      {posts.map(post => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  );
}

ReactDOM.render(
  <Reddit />,
  document.querySelector("#root")
);
Comment

when to use useEffect

What does useEffect do? By using this Hook, you tell React that 
your component needs to do something after render. 
React will remember the function you passed (we’ll refer to it 
as our “effect”), and call it later after performing the DOM updates.
Comment

useEffectOnce

import {useEffectOnce} from 'react-use';

const Demo = () => {
  useEffectOnce(() => {
    console.log('Running effect once on mount')
    
    return () => {
      console.log('Running clean-up of effect on unmount')
    }
  });

  return null;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: react steam auth 
Javascript :: res : [ Circular ] nodejs 
Javascript :: jest-badges-readme 
Javascript :: jquery live notification 
Javascript :: super slider js 
Javascript :: Compare unequality of two operands. 
Javascript :: purecomponent re rendering 
Javascript :: _40 0 _55 null _65 0 _72 null react native fetch 
Javascript :: what is code.jquery integrity crossorigin 
Javascript :: select xml child element with jQuery 
Javascript :: Cypress.currentTest 
Javascript :: dropdown using ajax and django 
Javascript :: missing json after pyinstaller 
Javascript :: load bmfont three with webpack 
Javascript :: mvc form client side validation result callback 
Javascript :: vue change input value from console 
Javascript :: mindfusion calendar 
Javascript :: facebook game files example 
Javascript :: typeorm tosql 
Javascript :: automatic jquery interceptor with token 
Javascript :: Textbelt FOR mac 
Javascript :: jquery copier dans le presse papier 
Javascript :: @use-it/event-listener npm 
Javascript :: es6 strip child is null from object 
Javascript :: angular 4200 on ec2 access is localhost 
Javascript :: cannot create an instance of an abstract class httphandler angular 
Javascript :: why inspect tool display extra undefined 
Javascript :: sequelize body 
Javascript :: class in side class in jss 
Javascript :: transaction mode javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =