simple usestate example

function LoginForm() {
  const [form, setState] = useState({
    username: '',
    password: ''
  });

  const printValues = e => {
    e.preventDefault();
    console.log(form.username, form.password);
  };

  const updateField = e => {
    setState({
      ...form,
      [e.target.name]: e.target.value
    });
  };

  return (
    <form onSubmit={printValues}>
      <label>
        Username:
        <input
          value={form.username}
          name="username"
          onChange={updateField}
        />
      </label>
      <br />
      <label>
        Password:
        <input
          value={form.password}
          name="password"
          type="password"
          onChange={updateField}
        />
      </label>
      <br />
      <button>Submit</button>
    </form>
  );
}

5
2
Patrice Long 100 points

                                    // First: import useState. It's a named export from 'react'
// Or we could skip this step, and write React.useState
import React, { useState } from 'react';
import ReactDOM from 'react-dom';

// This component expects 2 props:
//   text - the text to display
//   maxLength - how many characters to show before &quot;read more&quot;
function LessText({ text, maxLength }) {
  // Create a piece of state, and initialize it to `true`
  // `hidden` will hold the current value of the state,
  // and `setHidden` will let us change it
  const [hidden, setHidden] = useState(true);

  // If the text is short enough, just render it
  if (text.length &lt;= maxLength) {
    return &lt;span&gt;{text}&lt;/span&gt;;
  }

  // Render the text (shortened or full-length) followed by
  // a link to expand/collapse it.
  // When a link is clicked, update the value of `hidden`,
  // which will trigger a re-render
  return (
    &lt;span&gt;
      {hidden ? `${text.substr(0, maxLength).trim()} ...` : text}
      {hidden ? (
        &lt;a onClick={() =&gt; setHidden(false)}&gt; read more&lt;/a&gt;
      ) : (
        &lt;a onClick={() =&gt; setHidden(true)}&gt; read less&lt;/a&gt;
      )}
    &lt;/span&gt;
  );
}

ReactDOM.render(
  &lt;LessText
    text={`Focused, hard work is the real key
      to success. Keep your eyes on the goal, 
      and just keep taking the next step 
      towards completing it.`}
    maxLength={35}
  /&gt;,
  document.querySelector('#root')
);

5 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
usestate in react functional component why we use usestate in react simple useState example usestate for function usestate react native example usestate react hooks how to use usestate hook usestate react explained react js usestate function react functional usestate useState with pure js reactjs usestate example using state hook how react useState hook work usestate function functional components using useState usestate in function react usestate in function in hook how to use useState method in react usestate hook in react react function usestate Usestates example usestate examples in react programs of usestate in react react component usestate example usestate simple example usestate tutorial in react how to write usestate in react usestate example in react js usestate sample usestate in react plain js project react usestate hook example best way to use Usestate hook usestate react example react native useState example what is usestate simple example basic usestate example react usestate examples react useState hook examples example of useState react hook usestate react examples sample useState react js react usestate example hookstate example usestate example in react usestate hook example usestate in react js example usestate in react tutorial usestatee example create usestate in pure js usestate react hook example useState example usestate react hooks example usestate( ) array useState hook with string properties add values usestate is usestate same as this.state? usestate hook for object how to usestate a number data type in javascript useState string turn into object usestate setter how to create array of react usestate hooks reactjs useState array usestate hook upsater UseSTate hook example array usestate hook example object How to useState Hook for for Object why usestate is use in react usestate for array react hooks useState call useStater array react useState with list react usestate list react usestate hook update string react usestate object example code react hooks usestate string setState array useState array functions with usestate react usestate with arrays react usestate on change let usestate useState hook with an array reactjs useState list do functional components have state reactjs hook functional component unable to get value from state in react functional const {} = this.state useState setState for array import * as react from 'react' usestate usestate hook sytnax react hooks merge state how does usestate hook changes state if it is defined as const how to update state on function how to make one object of diffrent useState values react native false to multiple variables using array with useState with prev value react native useState hook array difference between react.useState and usestate importing useState with array react merge new boolean value in react hooks merge new boolean value on react hooks const [indicador, setIndicador] = useState(0); react useState text array to useState let or usestate react native usestate list of objects usestate array example setting object in state with hooks functional component object state hook useState(true) react hooks example can we pass states and props using functions and usestate in react useState&lt;string&gt; react prevstate with useState example all false in react usestate hook setState hook function taking prevSate add useState alternate usestate react hook array state useState list how to set multiple state in functional component 2 useState store tu 1 state array usestate hook data type useState() hook for string useState with a lot of states examples usestate react object react usestate set state input value usestate boolean react js useState setValue object usestate hook with object functional component usestate for object with 2 values usestate multiple variables why would i use a useState hook usestate hook array react usestate array display usestate react react hooks usestate boolean react usestate update hook with new atributes usesatte with boolean usestate in array pushing state in prevstate array in usestate update the useState hook on click react useState read current state react usestate update react usestate with an object usestate hook spread operator prevState simple examples of useState hooks react useState set address update array how to use usestate variables in react assign value to usestate object functional react object usestate how to use arrays with useState intialize usestate effectly react usestate with let setstate usestate array react usestate hook for object hooks usestate object usestate react text in how to toggle through an array with the useState hook update object with react hook usestate string hooks usestate boolean how to add to render text react useState usestate() hook const [open, setOpen] = React.useState(false); alternate way to have a bunch of useState object in hook react hooks usestate object react hooks usestate to store array useState with an array to class state usestate in class component for array usestate hook list of objects A guide to useSate in React import use state update values in usestate hook react usestate false import react usestate from 'react' usestate object example usestate with array usestate array how to import useState with previous react usestate object what is usestate in react to store multiple type of value in useState array how to enter two words in usestate react useState of array use state in react example useState as an array react js send usestate boolean value to another file usestate for object array useSTATE how to use usestate for button in react usestate object react usestate hook explained usestate button
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source