React
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.text {
width: 100%;
padding: 3px;
font-size: 16px;
box-sizing: border-box;
margin-top: 1rem;
}
.output {
font-size: 15px;
color: #444;
margin-top: 1rem
}
React
class Weirdness extends React.Component {
constructor(props) {
super(props)
this.state = {
stuff: ''
}
this.onChange = this.onChange.bind(this);
}
onChange(e) {
this.setState({
stuff: e.target.value
});
console.log('[Length of Stuff is]:', this.state.stuff.length);
}
render() {
return (
<div>
<h2>Weird length reporting in the console</h2>
<i style={{fontSize: "12px"}}>Open the console and type a word. The rendered count is shown below and is what you expect. Now hit the delete key to watch the length count INCREASE in the console!</i>
<input
type="text"
className="text"
name="stuff"
placeholder="Type a word and watch the weirdness"
onChange={this.onChange}
value={this.state.stuff}
>
</input>
<div className="output">The rendered length of stuff is : {this.state.stuff.length}</div>
</div>
);
}
}
ReactDOM.render(<Weirdness />, document.querySelector("#app"))