ReactJS Foundations

Listing 6-23: Initializing State in a Function Component

Previous Listing | Next Listing

Headlines for 5/1/2025, 12:53:24 AM

...
import {useState} from 'react'

function NewsFeed3(props) {

const [date,setDate] = useState(new Date());
const [headlines,setHeadlines] = useState([]);
  
  return(
    <>
      <h1>Headlines for {date.toLocaleString()}</h1>
      ...
    </>
  )
}

export default NewsFeed3;

Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.

ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.

React JS Foundations

Loading...