Skip to content

Example state#14

Open
elineka wants to merge 3 commits intomainfrom
exampleState
Open

Example state#14
elineka wants to merge 3 commits intomainfrom
exampleState

Conversation

@elineka
Copy link
Copy Markdown
Owner

@elineka elineka commented Jul 19, 2021

No description provided.

@elineka elineka linked an issue Jul 19, 2021 that may be closed by this pull request
@elineka elineka removed a link to an issue Jul 19, 2021
@elineka elineka closed this Jul 20, 2021
@elineka elineka reopened this Jul 20, 2021
@elineka elineka requested a review from bmsteven July 20, 2021 07:35
@elineka elineka linked an issue Jul 20, 2021 that may be closed by this pull request

function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example on how to declare and update state with objects or array datatypes?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some Examples
In each of these example learn to add, edit, delete a single item in the state

// arrays
const [todos, setTodos] = useState (["Learn hooks", "learn contextapi"])

// arrays of object
const [todos, setTodos] = useState ([
  {
    id: 1,
    name: "Learn hooks!",
  },
   {
    id: 2,
    name: "Learn hooks!",
  },
]}

// objects
const [formData, setFormData] = useState({
  username: "",
  password: ""
})

// objects that contains array
const [person, setPerson] = useState({
  id: 2,
  name: "Baraka",
  age: 28,
  followers: [
     {
        id: 3,
        name: "Winston",
        age: 43
      },
     {
        id: 15,
        name: "Anna",
        age: 22
      },
  ]
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

State

2 participants