React App
hyunu0504.github.io
<input ref={login} > </input>\
<div ref={login}> </div>
import { useEffect, useRef, useState } from "react";
import "./App.css";
function App() {
const loginBtn = () => {
alert(`안녕하세요 ${login.current.value} 님`);
login.current.focus();
};
const [count, setCount] = useState(0);
const nums = useRef(0);
const login = useRef();
useEffect(() => {
login.current.focus();
}, []);
return (
<div className="App">
<div>useState : {count}</div>
<div>useRef : {nums.current}</div>
<button onClick={() => setCount(count + 1)}>useState버튼</button>
<button onClick={() => (nums.current += 1)}>useRef버튼</button>
<p />
<form>
<input ref={login} type="text" placeholder="사용자명"></input>
<button onClick={loginBtn}>Login</button>
</form>
</div>
);
}
export default App;
[React] 리액트 훅( useMemo, useCallback ) (0) | 2022.07.19 |
---|---|
[React] 리액트 훅( useReducer ) (0) | 2022.07.16 |
[React] 리액트 훅( Context API ) (0) | 2022.07.10 |
[React] props (0) | 2022.07.09 |
[React] 리액트 훅( useEffect ) (0) | 2022.07.06 |