Quantcast
Channel: How do you change what jsx blocks are being rendered from inside a same class? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by LukeT for How do you change what jsx blocks are being rendered from inside a same class?

$
0
0

You have the right idea - but in a class there is only one render method. Your logic does belong inside the render. This should do what you're looking for:

class Flicker extends React.Component {    constructor(props){        super(props);        this.state = {mode: "On"};    }    flipOn(){        this.setState({mode: "On"})    }    flipOff(){        this.setState({mode: "Off"})    }    render () {        return (<button onClick={(this.state.mode === 'Off') ? this.flipOn : this.flipOff}>State: {this.state.mode}</button>        )    }}export default Flicker;

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>