Skip to content

Instantly share code, notes, and snippets.

@wwiechorek
Created August 8, 2019 20:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wwiechorek/c80fd9c8f7102ac5e30b3497e2a2fa0f to your computer and use it in GitHub Desktop.
HooksError
import React, { useState } from 'react'
function AppHooks() {
const [list, setList] = useState([])
function run() {
setInterval(() => {
list.push(Math.random())
setList([...list])
}, 1000)
}
return (
<div>
{list.length}
<button onClick={ () => run() }>Run</button>
</div>
);
}
class AppClass extends React.Component {
state = {
list: []
}
run() {
setInterval(() => {
let list = this.state.list
list.push(Math.random())
this.setState({
list
})
}, 1000)
}
render() {
return (
<div>
{this.state.list.length}
<button onClick={ () => this.run() }>Run</button>
</div>
)
}
}
function App() {
return (
<React.Fragment>
<h1>Hooks</h1>
<AppHooks />
<h1>Class</h1>
<AppClass />
</React.Fragment>
)
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment