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.
|
||||
|
||||
```jsx
|
||||
class PasswordRevealer extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
shown: false
|
||||
function PasswordRevealer(props) {
|
||||
const { value } = props;
|
||||
|
||||
const [shown, setShown] = useState(false);
|
||||
const toggleShown = () => {
|
||||
setShown(!shown);
|
||||
};
|
||||
this.toggleShown = this.toggleShown.bind(this);
|
||||
}
|
||||
|
||||
toggleShown() {
|
||||
this.setState(state => ({ shown: !state.shown }));
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<input
|
||||
type={this.state.shown ? 'text' : 'password'}
|
||||
value={this.props.value}
|
||||
type={shown ? "text" : "password"}
|
||||
value={value}
|
||||
onChange={() => {}}
|
||||
/>
|
||||
<button onClick={this.toggleShown}>Show/Hide</button>
|
||||
<button onClick={() => toggleShown()}>Show/Hide</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user