I re-organized your code a bit to work as a functional component. As others were commenting, you need to use state to re-render your components on updates. I removed count since .map allows for indexing.( you can add it back as a variable inside TaskFunction if you deem necessary )

W3 School's example works because they are not updating state. They have a list that is pre-set with values and render those values.

Lastly, your map function need to 'return' the element. As in the w3 school's example, map returns the Car component. So what you had was a little mixed up.

Hope this helps.

function App() {
  const [tasks, setTasks] = React.useState([])
  const generateValue=()=>{
    let someValue = Math.random() *10
    let newValue = {value: someValue}
    setTasks([...tasks, newValue])
  }
  return (
    <div>
      <button onClick={()=>{generateValue()}}>Add New Item</button>
      <h1>List</h1>
      <ul>
        {tasks.map((task, index)=>(<li key={index}>{task.value}</li>))}
      </ul>
    </div>
  )
}

ReactDOM.createRoot(document.querySelector("#app")).render(<App />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>

In a comment you ask to see the state example using the old style class components -

class App extends React.Component {
  state = {
    tasks: []
  }
  generateValue = () => {
    let someValue = Math.random() *10
    let newValue = {value: someValue}  
    this.setState({ tasks: [ ...this.state.tasks, newValue ] })
  }
  render() {
    return (
      <div>
        <button onClick={this.generateValue}>Add New Item</button>
        <h1>List</h1>
        <ul>
          {this.state.tasks.map((task, index)=>(<li key={index}>{task.value}</li>))}
        </ul>
      </div>
    )
  }
}

ReactDOM.createRoot(document.querySelector("#app")).render(<App />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>

Without public class fields -

class App extends React.Component {
  constructor() {
    super()
    this.state = {
      tasks: []
    }
  }
  generateValue() {
    let someValue = Math.random() *10
    let newValue = {value: someValue}  
    this.setState({ tasks: [ ...this.state.tasks, newValue ] })
  }
  render() {
    return (
      <div>
        <button onClick={this.generateValue.bind(this)}>Add New Item</button>
        <h1>List</h1>
        <ul>
          {this.state.tasks.map((task, index)=>(<li key={index}>{task.value}</li>))}
        </ul>
      </div>
    )
  }
}

ReactDOM.createRoot(document.querySelector("#app")).render(<App />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>

Answer from amandarose on Stack Overflow
🌐
W3Schools
w3schools.com › react › react_es6_array_map.asp
React ES6 Array map()
React Compiler React Quiz React Exercises React Syllabus React Study Plan React Server React Interview Prep ... The map() method creates a new array with the results of calling a function for every array element.
🌐
W3Schools
w3schools.com › react › react_es6_array_methods.asp
React ES6 Array Methods
The .map() method allows you to run a function on each item in the array, returning a new array as the result. In React, map() can be used to generate lists. ... Coding fundamentals as a game.
Discussions

Having problem with Array.map() in React when I try to print all array items in unordered list
And since I was learning from w3schools, you can see that my commented line is exactly like their's in their React tutorial, but the funny thing is that it is working in their program but not in mine. This is the line I'm talking about that is in my code : {tasks.map((msg) => More on stackoverflow.com
🌐 stackoverflow.com
reactjs - Map over an array in React - Stack Overflow
I am building an app that displays video games and various info about them, pulling data from an API. I am trying to display all of the platforms that the game is playable on, PlayStation, Xbox, et... More on stackoverflow.com
🌐 stackoverflow.com
Rendering an array.map() in React - javascript
I am having a problem where I am trying to use array of data to render a element. In the code below the console logs are working fine, but the list items aren't appearing. var Main = React. More on stackoverflow.com
🌐 stackoverflow.com
React - Use Array.map() to Dynamically Render Elements - .map is different than in js
{item} ) normally JavaScript wouldn’t spit anything out of that… an object surrounded by strings without quotation marks… The previous challenges stated that we use normal js in render method, before return statement. So can anybody explain to me what this line is ? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
8
0
January 25, 2023
🌐
W3Schools
w3schools.com › REACT › showreact.asp
W3Schools online REACT editor
import React from 'react'; import ReactDOM from 'react-dom/client'; const myArray = ['apple', 'banana', 'orange']; const myList = myArray.map((item) => <p>{item}</p>) const container = document.getElementById('root'); const root = ReactDOM.createRoot(container); root.render(myList); <!doctype html> <html lang="en"> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html> ⬤ ⬤ ⬤ ·
🌐
W3Schools
w3schoolsua.github.io › react › react_es6_array_methods_en.html
React ES6 Array Methods. Lessons for beginners. W3Schools in English
The .map() method allows you to run a function on each item in the array, returning a new array as the result. In React, map() can be used to generate lists.
🌐
Medium
medium.com › @johnsonzagazor06 › understanding-the-basics-of-map-and-arrays-in-react-d380f37764bc
Understanding the Basics of map() and Arrays in React | by Okafor Johnson | Medium
September 10, 2024 - Inside the return statement, we use the map() method to iterate over the fruits array. For each fruit, we create an <li> element and assign a unique key prop (here, using the index, but it's better to use a unique identifier if available).
🌐
W3Schools
w3schools.com › react › react_lists.asp
React Lists - React Fundamentals
If you need a refresher on the map() method, check out the ES6 Array map() section. Let's create a simple list using the map() method: function MyCars() { const cars = ['Ford', 'BMW', 'Audi']; return ( <> <h1>My Cars:</h1> <ul> {cars.map((car) => <li>I am a { car }</li>)} </ul> </> ); } createRoot(document.getElementById('root')).render( <MyCars /> ); ... When you run this code in your React environment, it will work but you will receive a warning that there is no "key" provided for the list items.
Top answer
1 of 1
1

I re-organized your code a bit to work as a functional component. As others were commenting, you need to use state to re-render your components on updates. I removed count since .map allows for indexing.( you can add it back as a variable inside TaskFunction if you deem necessary )

W3 School's example works because they are not updating state. They have a list that is pre-set with values and render those values.

Lastly, your map function need to 'return' the element. As in the w3 school's example, map returns the Car component. So what you had was a little mixed up.

Hope this helps.

function App() {
  const [tasks, setTasks] = React.useState([])
  const generateValue=()=>{
    let someValue = Math.random() *10
    let newValue = {value: someValue}
    setTasks([...tasks, newValue])
  }
  return (
    <div>
      <button onClick={()=>{generateValue()}}>Add New Item</button>
      <h1>List</h1>
      <ul>
        {tasks.map((task, index)=>(<li key={index}>{task.value}</li>))}
      </ul>
    </div>
  )
}

ReactDOM.createRoot(document.querySelector("#app")).render(<App />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>

In a comment you ask to see the state example using the old style class components -

class App extends React.Component {
  state = {
    tasks: []
  }
  generateValue = () => {
    let someValue = Math.random() *10
    let newValue = {value: someValue}  
    this.setState({ tasks: [ ...this.state.tasks, newValue ] })
  }
  render() {
    return (
      <div>
        <button onClick={this.generateValue}>Add New Item</button>
        <h1>List</h1>
        <ul>
          {this.state.tasks.map((task, index)=>(<li key={index}>{task.value}</li>))}
        </ul>
      </div>
    )
  }
}

ReactDOM.createRoot(document.querySelector("#app")).render(<App />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>

Without public class fields -

class App extends React.Component {
  constructor() {
    super()
    this.state = {
      tasks: []
    }
  }
  generateValue() {
    let someValue = Math.random() *10
    let newValue = {value: someValue}  
    this.setState({ tasks: [ ...this.state.tasks, newValue ] })
  }
  render() {
    return (
      <div>
        <button onClick={this.generateValue.bind(this)}>Add New Item</button>
        <h1>List</h1>
        <ul>
          {this.state.tasks.map((task, index)=>(<li key={index}>{task.value}</li>))}
        </ul>
      </div>
    )
  }
}

ReactDOM.createRoot(document.querySelector("#app")).render(<App />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>

Find elsewhere
🌐
Scrimba
scrimba.com › articles › react-list-array-with-map-function
How to use Array.map to render a list of items in React
November 1, 2022 - The Array.map method is a powerful higher-order function that lets you transform all the values in an array using a mapping function. This is especially useful for inserting a list of data into a React app, since you can't use for loops.
🌐
Hackingwithreact
hackingwithreact.com › read › 1 › 13 › rendering-an-array-of-data-with-map-and-jsx
Rendering an Array of Data with map() and JSX – a free Hacking with React tutorial
There's one more thing we're going to cover before you know enough React basics to be able to move on to a real project, and that's how to loop over an array to render its contents. Right now we have a single person with a single country, but wouldn't it be neat if we could have 10 people with 10 countries, and have them all rendered? Sure it would. Luckily for us, this is easy to do in JSX thanks to an array method called map().
🌐
Atomizedobjects
atomizedobjects.com › blog › react › how-to-render-an-array-of-objects-with-map-in-react
How to render an array of objects with Array.map in React | Atomized Objects
The Array.map() method is key to rendering objects and data in react and it is a prototype function on all arrays in JavaScript.
🌐
Ordinarycoders
ordinarycoders.com › blog › article › javascript-react-map-method
Using map() in JavaScript and React.js
So the map() method is a commonly used JavaScript array method. As stated in this tutorial, map() is used to create lists in React. It can be used to render an array as a list or to create dynamic lists as we learned in this tutorial.
🌐
C# Corner
c-sharpcorner.com › blogs › map-method-in-react-js
Map Method In React JS
April 28, 2022 - The map() method creates a new array by calling a provided function on every element in the calling array. Add a new file in src folder and named it as Mapdemo.js as below. import React,{Component } from 'react' function MapDemo(){ const array1 ...
🌐
GitHub
github.com › Asabeneh › 30-Days-Of-React › blob › master › 06_Day_Map_List_Keys › 06_map_list_keys.md
30-Days-Of-React/06_Day_Map_List_Keys/06_map_list_keys.md at master · Asabeneh/30-Days-Of-React
If the data does not have an id we have to find a way to create a unique identifier for each element when we map it. See the following example: import React from 'react' import ReactDOM from 'react-dom' const Numbers = ({ numbers }) => { // modifying array to array of li JSX const list = numbers.map((num) => <li key={num}>{num}</li>) return list } const App = () => { const numbers = [1, 2, 3, 4, 5] return ( <div className='container'> <div> <h1>Numbers List</h1> <ul> <Numbers numbers={numbers} /> </ul> </div> </div> ) } const rootElement = document.getElementById('root') ReactDOM.render(<App />, rootElement)
Author: Asabeneh
🌐
freeCodeCamp
freecodecamp.org › news › how-to-render-lists-in-react
How to Render Lists in React using array.map()
April 10, 2023 - To get the names of the applicants, you can easily do that with JavaScript's array.map method. Below is how you can map every applicant's name: import React from 'react'; const applicants = [ { name: 'Joe', work: 'freelance-developer', blogs: '54', websites: '32', hackathons: '6', location: 'morocco', id: '0', }, { name: 'janet', work: 'fullstack-developer', blogs: '34', websites: '12', hackathons: '8', location: 'Mozambique', id: '1', }, ]; function App() { return ( <> {applicants.map(function(data) { return ( <div> Applicant name: {data.name} </div> ) })} </> ) } export default App;
🌐
CodeChef
codechef.com › learn › course › react-js › CREACT026 › problems › PREACT0121
Map() Method in Javascript in React JS for Front-end development
It's like telling JavaScript: “Hey, go through this array, do something to each item, and give me back a new array with the results.” The original array stays untouched, which is great when you want to transform data without modifying the source. ... let numbers = [5, 10, 15]; let divided = numbers.map(num => num / 5); console.log(divided); // Output - [1, 2, 3]
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
React - Use Array.map() to Dynamically Render Elements - .map is different than in js
January 25, 2023 - {item} ) normally JavaScript wouldn’t spit anything out of that… an object surrounded by strings without quotation marks… The previous challenges stated that we use normal js in render method, before return statement. So can anybody explain to me what this line is ?
🌐
React
legacy.reactjs.org › docs › lists-and-keys.html
Lists and Keys – React
We can refactor the previous example into a component that accepts an array of numbers and outputs a list of elements. function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) => <li>{number}</li> ); return ( <ul>{listItems}</ul> ); } const numbers = [1, 2, 3, 4, 5]; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<NumberList numbers={numbers} />);
🌐
Medium
medium.com › @hammadrao891 › understanding-the-map-function-in-react-a-comprehensive-guide-887cee7f7955
Understanding the Map Function in React: A Comprehensive Guide | by Hammad Rao | Medium
December 14, 2023 - - `element`: The current element being processed in the array. - `index`: The index of the current element in the array. ... The primary use case for the `map` function in React is rendering lists of elements dynamically.