Created
September 22, 2019 23:02
-
-
Save matthew-gerstman/e43deb4d68a23c4e907ddbb426f2e1bf to your computer and use it in GitHub Desktop.
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 from 'react'; | |
import './App.css'; | |
class App extends React.Component { | |
state = {anonymousFunctions: true, count: 100}; | |
updateCount = e => { | |
this.setState({count: e.target.value}); | |
}; | |
updateAnonymous = () => { | |
this.setState({anonymousFunctions: !this.state.anonymousFunctions}); | |
}; | |
render() { | |
const {anonymousFunctions, count} = this.state; | |
console.log({anonymousFunctions}); | |
return ( | |
<div className="App"> | |
<h1>{count}</h1> | |
<div> | |
<label>Num Fields: </label> | |
<input value={count} onChange={this.updateCount} /> <br /> | |
<label>Use Anonymous Functions</label> | |
<input | |
type="checkbox" | |
checked={anonymousFunctions} | |
onChange={this.updateAnonymous} | |
/> | |
</div> | |
<br /> | |
{anonymousFunctions ? ( | |
<AnonymousNumberList count={count} /> | |
) : ( | |
<NumberList count={count} /> | |
)} | |
</div> | |
); | |
} | |
} | |
export default App; | |
function AnonymousNumberList({count}) { | |
let list = []; | |
for (let i = 0; i < count; i++) { | |
list.push(<Number getNumber={() => i} key={i} />); | |
} | |
return list; | |
} | |
function NumberList({count}) { | |
let list = []; | |
for (let i = 0; i < count; i++) { | |
list.push(<Number number={i} key={i} />); | |
} | |
return list; | |
} | |
function Number({number, getNumber}) { | |
return ( | |
<h1 style={{color: number != null ? 'red' : 'blue'}}> | |
{number != null ? number : getNumber()} | |
</h1> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment