useref in functional component
/*
A common use case is to access a child imperatively:
*/
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}import React, { useRef } from 'react';
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}const refContainer = useRef(initialValue);
//useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue).
//The returned object will persist for the full lifetime of the component.
Also in JavaScript:
- how to install all needed modules in node
- adonis andwhere
- Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
- javascript keywords
- forEach modify array JavaScript
- style an element with javascript
- train function ajax
- how to find out what a string ends with in javascript
- react useLocation()
- how to get data in json format in javascript
- javascript push and concat
- remove extra space in string javascript
- javascript onsubmit form validation
- javascript redirect page
- js immutable update object
- express error handling
- sass variables across files
- call javascript function from php
- angular 9 change menu link item at runtime
- javascript compare two arrays of objects get same elements
- javascript create object from key value pairs
- manifest.webmanifest example
- ajax laravel get values from form
- Javascript clear canvas