PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] why when i put what i pass in props in useState i have an empty array?

 October 28, 2022     is-empty, react-hooks, reactjs, use-state     No comments   

Issue

in the child component , I can read the props data in the console.log but when i put the state data in useState and i put the state in a console.log , i have an empty array and i don't understand why ?

    const FicheUserDisplay = ({ data }) => {
console.log("-->data<--");
console.log(data);

const [state, setState] = useState(data);
console.log("-->State");

console.log("state dans composant Fiche User")
console.log(state);

  return (

enter image description here


Solution

Try in this way, you are using antipattern for react environment

useEffect(() => {
    if (data) {
      setState(data);
    }
  }, [data]);


Answered By - Evren
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing