Skip to content

Instantly share code, notes, and snippets.

@npverni
Created February 23, 2018 21:44
Show Gist options
  • Save npverni/7b6f300aea0535c84bb6e0e725f89162 to your computer and use it in GitHub Desktop.
Save npverni/7b6f300aea0535c84bb6e0e725f89162 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import './App.css';
class App extends Component {
state = {
focused: '',
}
setFocus = field => this.setState({ focused: field });
unsetFocus = () => console.log("Unsetting focus");
isFocused = field => (this.state.focused === field);
render() {
return (
<div className="App">
<div>
<label>Name</label>
<input
type="text"
onFocus={() => this.setFocus('name')}
onBlur={this.unsetFocus}
/>
<small>{this.isFocused('name') && "Focused on name"}</small>
</div>
<div>
<label>Age</label>
<input
type="text"
onFocus={() => this.setFocus('age')}
onBlur={this.unsetFocus}
/>
<small>{this.isFocused('age') && "Focused on age"}</small>
</div>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment