[React Fundamentals] State Basics

State is used for properties on a component that will change, versus static properties that are passed in. This lesson will introduce you to taking input within your React components.

Unlike props, which are meant to be passed into our component as static values or methods, state is a collection of values that's meant to be managed by our component itself. 

import React from 'react';

export default class App extends React.Component {
    constructor(){
        super(); //This is going to give us our context for this within our component
        this.state = {
            txt: 'State',
            count: 1
        }
    }
    update(e){
        this.setState({
            txt: e.target.value
        })
    }
    render() {
        return (
            <div>
                <input type="text" onChange={this.update.bind(this)} />
                <span>Hello {this.state.txt}</span>
            </div>
        )
    }
}
原文地址:https://www.cnblogs.com/Answer1215/p/5771544.html