Okay So i have multiple question, (more or less basic ones)
First Question: When I do something like
class App extends Component { state = { person: [ {id: "name1n", name: "Rohit", age: 24}, {id: "name2l", name: "Hariom", age: 23}, {id: "name3g", name: "Vaibhav", age: 58} ], someOtherState: "Untouched state", showPerson: false } var apple = 10; Before render, it throws an error saying failed to compiler var apple but if I do same thing inside render like render( var apple = 10; ); it does not throw the error. Can someone please explain the reason for same?
Second Question Inside render, when I do something like this
render() { const style = { width: '150px', margin: 'auto', color: 'white', padding: '5px', backgroundColor: 'green', marginBottom: '10px' }; var apple = 10; console.log(apple) // -> This log let person = null; if (this.state.showPerson) { person= ( <div> { this.state.person.map((el, index) => { return <Person key={el.id} click={this.deletePersonHandler.bind(index)} name={el.name} age={el.age} changed={(event) => this.eventSwitchHandler(event, el.id)} /> }) } </div> ); style.backgroundColor = 'red' apple = 20; console.log(apple) // -> This log } return ( <div className="App"> <h1> Hi I am react App</h1> <button style={style} onClick={this.togglerPersonHandler}>Button</button> {person} </div> ) } } it logs 10 twice in console before displaying 20, The question being why is it displaying the number 10 twice before displaying 20? Shouldn't it just console.log -> 10 and then 20? instead of 10 -> 10 -> 20?
Third Question when I click on button it changes the BackgroundColor to red and when I again click it, it reverts back to green colo This is my togglePersonHandler which changes the state (showPerson) of If condition when button is clicked
togglerPersonHandler = () => { const doesShow = this.state.showPerson; this.setState({ showPerson: !doesShow }); } My third question is, Why does it changed by itself to its original color (green) when I click it back again? I usually used to do it with else condition