Guest User

Untitled

a guest
Jan 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class ControlledInput extends React.Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = { value: props.initialValue || '' }
  5. }
  6.  
  7. componentDidUpdate(prevProps, prevState) {
  8. console.log(prevProps.initialValue, this.props.initialValue)
  9. if (prevProps.initialValue !== this.props.initialValue) {
  10. this.setState({ value: this.props.initialValue || '' })
  11. }
  12. }
  13.  
  14. onChange = (ev) => {
  15. const { value } = ev.target
  16. this.setState({
  17. value
  18. }, () => {
  19. if (this.props.onChange) {
  20. this.props.onChange(this.state.value)
  21. }
  22. })
  23. }
  24.  
  25. render() {
  26. const { initialValue, tag, onChange, ...rest } = this.props
  27. console.log('value', initialValue)
  28. const props = {
  29. ...rest,
  30. onChange: this.onChange,
  31. value: this.state.value,
  32. }
  33. return React.createElement(tag, props)
  34. }
  35. }
Add Comment
Please, Sign In to add comment