I'd like to be able to increase opacity of a DOM element on click of a button.
Wrapping my head around React's concepts, I think this should be done through using state
and a functional setState()
function.
However, I keep running into an error with the following code:
import React, { Component } from 'react'
class ThoughtfulThoughts extends Component {
state = {
opacity: 0.4,
thoughts:
[
"live and let leave",
"just pee yourself",
"who knows what 'laf' is?"
]
}
makeAppear = () => {
// TODO: this causes a "RangeError: Maximum call stack size exceeded"
this.setState(prevState => ({
opacity: prevState.opacity + 0.2
}))
}
render() {
return (
<div className="thoughtful-thoughts">
<div className="current-thought" style={{opacity: this.state.opacity}}>
{this.state.thoughts.map((thought, i) => (<p>{thought}</p>))}
</div>
<button onClick={this.makeAppear()}>Think for me</button>
</div>
)
}
}
export default ThoughtfulThoughts
I don't want to use jQuery, nor direct DOM manipulation, nor CSS transitions, but would like to do this just in JS in "the React way".
Any pointers would be greatly appreciated!