Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to use useState Hook in React/ReactNative

import React, { useState } from 'react';

function StepTracker() {
  const [steps, setSteps] = useState(0);

  function increment() {
    setSteps(prevState => prevState + 1);
  }

  return (
    <div>
      Today you've taken {steps} steps!
      <br />
      <button onClick={increment}>
        I took another step
      </button>
    </div>
  );
}

ReactDOM.render(
  <StepTracker />,
  document.querySelector('#root')
);
Comment

usestate in react

  const [x, setx] = useState(0);
Comment

How to usestate in react

...
const [count, setCounter] = useState(0);
const [moreStuff, setMoreStuff] = useState(...);
...

const setCount = () => {
    setCounter(count + 1);
    setMoreStuff(...);
    ...
};
Comment

usestate in react

function handleOrangeClick() {
    // Similar to this.setState({ fruit: 'orange' })
    setFruit('orange');
  }
Comment

How to use React useState Hook

useState - Has the main purpose to change an element status
useState basically helps React to know what to elements need to be rerendered. 

Ex: We have a post                 (state: it is displayed) 
    You want to eliminate the post (setState: it is not displayed)

Code Ex.:
const [blogs, setBlogs] = useState([  
    { title: 'React forever', body:'Lorem ipsum...', author: 'Sara', id: 1 },
    { title: 'Vue kinda sucks', body:'Lorem ipsum...', author: 'Tony', id: 2 },
    { title: 'Angular lets not go there', body:'Lorem ipsum...', author: 'John', id: 3 },
  	]);

// The JS function that filters all the post with diff. id from the id of the post clicked
const handleDelete = (id) => {
  const newBlogs = blogs.filter(blog => blog.id !== id);
    setBlogs(newBlogs);
  }

// React renders the following JSX
return (
    <div className="blog-list">
      <h2>{ title }</h2>
        {blogs.map((blog) => (
          <div className="blog-preview" key={blog.id}>
            <h3>{ blog.title }</h3>
            <p>written by { blog.author }</p>
            <p>{ blog.body }</p> 
            <button onClick={() => handleDelete(blog.id)} className="hideBtn">Hide Post</button>
          </div>
        ))}
    </div>
    );
  
Comment

usestate in react

The initial value will be assigned only on the initial render (if it’s a function, it will be executed only on the initial render).
Comment

PREVIOUS NEXT
Code Example
Javascript :: async in useeffect 
Javascript :: touppercase javascript array 
Javascript :: useeffect hook react 
Javascript :: insert variable in string javascript 
Javascript :: node version check in cmd 
Javascript :: npm yarn run shell script 
Javascript :: jquery get selected checkbox value array 
Javascript :: how to add up all numbers in an array 
Javascript :: agregar clase en jquery 
Javascript :: react change state async 
Javascript :: pdf with puppeteer 
Javascript :: load a config file discordjs 
Javascript :: regex is empty string javascript 
Javascript :: docker react 
Javascript :: como ler um arquivo json com javascript 
Javascript :: angular 9 form value changes 
Javascript :: golang parse jason 
Javascript :: jquery checkbox unchecked 
Javascript :: jquery if .val is blank 
Javascript :: reactjs make main div scrollable 
Javascript :: js string contains 
Javascript :: how to hash with crypto Node.js 
Javascript :: clear file upload jquery 
Javascript :: comments in json 
Javascript :: delete element 
Javascript :: firebase app named default already exists react native 
Javascript :: alphabet string javascript 
Javascript :: redux devtools extention in redux toolkit 
Javascript :: duplicate value removed in array of object in javascript 
Javascript :: datepicker strart with monday 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =