When I read the ReactJS documentation it recommends using state to create controlled components. However, the below uncontrolled code is much cleaner and works flawlessly. I can understand why the parent component should use state, but the below code is so clean that I don't understand why controlled components are recommended. The "MyModule" prefix is optional and I don't even have to include that.
module MyModule {
export let userName: string = "";
interface iProps {
userName?: string;
}
export class Build extends React.Component<iProps, {}> {
constructor(props: iProps) {
super(props);
if (props.userName) {
MyModule.userName = props.userName;
}
}
handleChange(event) {
MyModule.userName = event.target.value;
}
render() {
return (<input type="text" defaultValue={MyModule.userName} onChange={this.handleChange.bind(this)} /> );
}
} //end class.
} //end module.