I am working on a task where I need to change component background by clicking on a button and changing state. I am not supposed to use React Hooks. So far I've got:
import React from 'react'
import ReactDOM from 'react-dom'
class Main extends React.Component {
constructor(props) {
super(props)
}
render() {
const {
colorChange,
styles
} = this.props
const text = this.props.data
return (
<main style={styles} >
<h1>welcome</h1>
<button
onClick={colorChange}
>
Change color
</button>
</main>
)
}
}
class App extends React.Component {
state = {
styles: { backgroundColor: 'white' },
}
colorChange = () => {
let backgroundColor = 'white'
let changedColor = 'black'
let backGround = this.state.styles==='white' ? changedColor : backgroundColor
this.setState({backGround})}
render() {
return (
<Main
colorChange={this.colorChange}
/>
)
}
}
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)
but that doesn't do anything. Where am I making a mistake?