cfprabhu
cfprabhu

Reputation: 5352

React js get the value from div

I am the newer for React js. How can i achieve the following logic using react js

Here is two div's and one button. When the user click the button these div should toggle .

Upvotes: 0

Views: 11458

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

You can use states, like this

var Component = React.createClass({
    getInitialState: function () {
        return {
            outsideText: 'Outside',
            insideText: 'Inside'
        }
    },

    handleClick: function () {
        this.setState({
            outsideText: this.state.insideText,
            insideText: this.state.outsideText
        })
    },

    render: function() {
        return <div>
            <div>
                { this.state.outsideText }
                <div>
                    { this.state.insideText }
                </div>
            </div>
            <button onClick={this.handleClick}>Change Text</button>
        </div>;
    }
});

Example

Upvotes: 3

Related Questions