Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

passing event handler to useEffeect

const App =() => {
  const [userText, setUserText] = useState("");

  useEffect(() => {
    const handleUserKeyPress = event => {
      const { key, keyCode } = event;

      if (keyCode === 32 || (keyCode >= 65 && keyCode <= 90)) {
        setUserText(`${userText}${key}`);
      }
    };

    window.addEventListener("keydown", handleUserKeyPress);

    return () => {
      window.removeEventListener("keydown", handleUserKeyPress);
    };
  }, [userText]); // ESLint will yell here, if `userText` is missing

  return (
    <div>
      <h1>Feel free to type!</h1>
      <blockquote>{userText}</blockquote>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
Comment

PREVIOUS NEXT
Code Example
Javascript :: round 2 decimales js 
Javascript :: js add data in object 
Javascript :: remove object from array of object 
Javascript :: charcodeat javascript 
Javascript :: discord.js if arguments null 
Javascript :: vue date filter component 
Javascript :: props navigation in class component 
Javascript :: get keys of object js 
Javascript :: counter in html and js 
Javascript :: javascript update value when slider moves javascript 
Javascript :: audio element javascript 
Javascript :: javascript array methods 
Javascript :: connect existing database with sequelize 
Javascript :: react click outside class implementation 
Javascript :: get width of screen 
Javascript :: server side rendering 
Javascript :: testing a function in jest on click react 
Javascript :: js check if map contains key 
Javascript :: start date time picker from day to year in html 
Javascript :: Substring in Javascript using substr 
Javascript :: lodash round 
Javascript :: nestjs Error: Cannot find module 
Javascript :: working of timers in javascript 
Javascript :: setCenter: not a LatLng or LatLngLiteral with finite coordinates: in property lat: not a number 
Javascript :: encrypt js 
Javascript :: jq get value without quotes 
Javascript :: convert UTC date to Indonesian local date format 
Javascript :: js if text contains lowercase 
Javascript :: decorators in javascript 
Javascript :: ordenar numeros array javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =