Search code examples
reactjsreact-hooksuse-stateis-empty

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


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]);