Skip to content

Instantly share code, notes, and snippets.

@xbrunosousa
Created August 16, 2020 12:56
Show Gist options
  • Save xbrunosousa/712fa10602d55b9284573b0dc22372d7 to your computer and use it in GitHub Desktop.
Save xbrunosousa/712fa10602d55b9284573b0dc22372d7 to your computer and use it in GitHub Desktop.
ExemploGrupoReactFb
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