Js.React.ComponentState

example

import React from 'react';


export class Counter extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 0
        }

        this.delta = this.delta.bind(this);
    }


    delta() {
        this.setState((prevState, props) => ({
            count: prevState.count + props.increment
        }));

    }


    render() {
        return (
            <div>
                <button onClick={this.delta}>
                    count
                </button>
                <div>
                    count is {this.state.count}
                </div >
            </div>
        )
    }
}

App.js

function App() {
  return (
    <div>
      <Counter increment = {1}/>
    </div>

  );
}
This entry was posted in Без рубрики. Bookmark the permalink.