-
-
Save wwiechorek/c80fd9c8f7102ac5e30b3497e2a2fa0f to your computer and use it in GitHub Desktop.
HooksError
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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