change password revealer component to hooks implementation
This commit is contained in:
@ -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>
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user