change password revealer component to hooks implementation

This commit is contained in:
komv8i
2019-02-07 11:22:58 -05:00
parent 509f7e87fa
commit 088b247ad9

View File

@ -8,30 +8,24 @@ In the`render()` method, use a`<div>` to wrap both the`<input>` and the `<button
Finally, bind the `<button>`'s `onClick` event to the `toggleShown` method. Finally, bind the `<button>`'s `onClick` event to the `toggleShown` method.
```jsx ```jsx
class PasswordRevealer extends React.Component { function PasswordRevealer(props) {
constructor(props) { const { value } = props;
super(props);
this.state = {
shown: false
};
this.toggleShown = this.toggleShown.bind(this);
}
toggleShown() { const [shown, setShown] = useState(false);
this.setState(state => ({ shown: !state.shown })); const toggleShown = () => {
} setShown(!shown);
};
render() { return (
return ( <div>
<div> <input
<input type={shown ? "text" : "password"}
type={this.state.shown ? 'text' : 'password'} value={value}
value={this.props.value} onChange={() => {}}
/> />
<button onClick={this.toggleShown}>Show/Hide</button> <button onClick={() => toggleShown()}>Show/Hide</button>
</div> </div>
); );
}
} }
``` ```