Skip to content

Instantly share code, notes, and snippets.

@HappyZombies
Created May 23, 2017 02:06
Show Gist options
  • Save HappyZombies/166fb21e979975067738b794030ac8a4 to your computer and use it in GitHub Desktop.
Save HappyZombies/166fb21e979975067738b794030ac8a4 to your computer and use it in GitHub Desktop.
React Materialize, Input component prop 'defaultValue' does not update on component re-render.
import React, { Component } from 'react'
import logo from './logo.svg'
import './App.css'
import MyForm from './MyForm'
class App extends Component {
constructor () {
super()
this.state = {name: 'Paul'}
}
componentDidMount () {
// After 2 seconds, update name state to John
setTimeout(function () {
this.setState({name: 'John'})
}.bind(this), 2000)
}
render () {
return (
<MyForm name={this.state.name} />
)
}
}
export default App
import React, { Component } from 'react'
import {Input} from 'react-materialize'
class MyForm extends Component {
componentWillUpdate (nextProp, nextState) {
console.log('Yes component should update')
}
render () {
alert(`Rendering MyForm, value of this.props.name is: ${this.props.name}`)
return (
<div>
<h1>{this.props.name}</h1>
<Input defaultValue={this.props.name} />
</div>
)
}
}
export default MyForm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment