Created
August 16, 2020 12:56
-
-
Save xbrunosousa/712fa10602d55b9284573b0dc22372d7 to your computer and use it in GitHub Desktop.
ExemploGrupoReactFb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useRef } from "react"; | |
export default () => { | |
const [state, setState] = useState({ | |
animate: false, | |
inputValue: "", | |
inputActive: false | |
}); | |
const inputRef = useRef(null); | |
const onClickBtn = () => { | |
setState((prevState) => ({ | |
...prevState, | |
inputActive: true, | |
animate: !prevState.animate, | |
inputValue: "" | |
})); | |
inputRef.current.focus(); | |
}; | |
return ( | |
<div> | |
<button onClick={onClickBtn}>Click</button> | |
<input | |
onChange={({ target }) => | |
setState((prevState) => ({ ...prevState, inputValue: target.value })) | |
} | |
value={state.inputValue} | |
className={`${state.inputActive ? "active" : ""} ${ | |
state.animate ? "animate" : "" | |
}`} | |
ref={inputRef} | |
placeholder="input" | |
/> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment