Skip to content

Instantly share code, notes, and snippets.

@watanabeyu
Created May 28, 2018 09:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save watanabeyu/ccf59b069987494a03a5f24d005a9857 to your computer and use it in GitHub Desktop.
Save watanabeyu/ccf59b069987494a03a5f24d005a9857 to your computer and use it in GitHub Desktop.
import React from 'react';
import { TextInput as Input } from 'react-native';
export default class TextInput extends React.Component {
static defaultProps = {
onFocus: () => { },
}
constructor(props) {
super(props);
this.state = {
value: this.props.value,
refresh: false,
};
}
shouldComponentUpdate(nextProps, nextState) {
if (this.state.value !== nextState.value) {
return false;
}
return true;
}
componentDidUpdate(prevProps) {
if (prevProps.value !== this.props.value && this.props.value === '') {
this.setState({ value: '', refresh: true }, () => this.setState({ refresh: false }));
}
}
onFocus = (e) => {
this.input.focus();
this.props.onFocus();
}
render() {
if (this.state.refresh) {
return null;
}
return (
<Input
{...this.props}
ref={(ref) => { this.input = ref; }}
value={this.state.value}
onFocus={this.onFocus}
/>
);
}
}
@hungvu193
Copy link

seem like it doesn't work with redux-form :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment