Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to render new value of array in react

Here I am getting my data from child component and trying to add it into existing array and also I am trying to display it in console using map but the moment I try to to do so I get nothing in console:

import "./App.css";
import Home from "./Components/Home";
import {useState} from 'react';

let App = () => {
  let DataList = ["Apple","Banana"];

  const newList = (data)=>{
      DataList = [...DataList,data];
  }


  return (
    <div>
      <Home newList = {newList}/>
      
      { DataList.map((val)=>(
          console.log(val)
        )) 
      }
    </div>
  )
};

export default App;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to use React hooks to make it work, then only your component will rerender

import "./App.css";
import Home from "./Components/Home";
import {useState} from 'react';

let App = () => {
 
  const [DataList, setDataList] = useState(["Apple","Banana"]);

  const newList = (data)=>{
      let temp = [...DataList,data];
      setDataList(temp);
  }


  return (
    <div>
      <Home newList = {newList}/>
      
      { DataList.map((val)=>(
          console.log(val)
        )) 
      }
    </div>
  )
};

export default App;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading