Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/30c2c043ed8c40a2fea696dc93842083 to your computer and use it in GitHub Desktop.
Save kenmori/30c2c043ed8c40a2fea696dc93842083 to your computer and use it in GitHub Desktop.
An Effect function must not return anything besides a function

if you occure below cord

index.js:2178 Warning: An Effect function must not return anything besides a function, which is used for clean-up.

It looks like you wrote useEffect(async () => ...) or returned a Promise. Instead, you may write an async function separately and then call it from inside the effect:

async function fetchComment(commentId) {
  // You can await here
}

useEffect(() => {
  fetchComment(commentId);
}, [commentId]);

make sure that your code at useEffect

now is

function Some() {
  const [posts, setPosts] = useState([]);
  useEffect(
    async () => {
      const res = await fetch('https://www.reddit.com/r/reactjs.json');
      const json = await res.json();
      setPosts(json.data.children.map(c => c.data));
    }
  ); 

fix to

function Some() {
  const [posts, setPosts] = useState([]);
  useEffect(
    async () => {
      const res = await fetch('https://www.reddit.com/r/reactjs.json');
      const json = await res.json();
      setPosts(json.data.children.map(c => c.data));
    },[setPosts]// add here
  ); 

This reason that useEffect called every render and call fetch, update state, after state update, render Component...and call fetch...

So that setPosts is call when component rendered only one. setPosts itself not update. stop useEffect

@JairajSinghKushwaha
Copy link

JairajSinghKushwaha commented Jun 12, 2022

Try to use async with explicit function inside useEffect().

const [posts, setPosts] = useState([]);

useEffect(() => {
async function fetchComment() {
const res = await fetch('https://www.reddit.com/r/reactjs.json');
const json = await res.json();
setPosts(json.data.children.map(x => x.data));
}
fetchComment()
}, [setPosts]);

======================|| Learn C# ||===========================

C# Constructor
C# Enum
C# String
C# Namespace
C# Casting

=========================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment