[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>
        )
    }
}
时间: 2024-12-25 08:29:51

[React Fundamentals] State Basics的相关文章

[React] Update State in React with Ramda&#39;s Evolve

In this lesson we'll take a stateful React component and look at how we can refactor our setState calls to use an updater function and then leverage Ramda's evolvefunction to make our updater function a reusable utility that isn't tied to the React A

React:Lifting State Up

在学习React的组件的时候,我也好奇组件间需要共享状态或通信的时候,React是如何处理的.在文档的QUICK START的提到Lifting State Up(状态提升),并不是什么新鲜东西.只是把几个组件需要共享的状态拿出来放到最近的父组件而已.然后通过传参的方式注入子组件,成为其props.由于子组件获取的状态state_inShare都来自父组件,因此各自的state_inShare是同步的. In React, sharing state is accomplished by mov

React给state赋值赋值的两种方法

如果你看过React的官方文档,就会对怎么给局部state赋值有一定的了解.如下代码: class Test extends React.Component { constructor(props) { super(props); this.state = { name:'李磊' }; // 为了在回调中使用 `this`,这个绑定是必不可少的 this.handleClick = this.handleClick.bind(this); } handleClick(){ this.setSta

react之state&amp;生命周期

在元素渲染章节中,我们了解了一种更新UI界面的方法,通过调用ReactDOM.render()修改我们想要的 元素 import ReactDOM from 'react-dom' class ClockCom extends React.Component{ render(){ return( <div> <h1>this clock component</h1> <h2>It is {this.props.time.toLocaleTimeString(

[React Fundamentals] Component Lifecycle - Mounting Basics

React components have a lifecycle, and you are able to access specific phases of that lifecycle. This lesson will introduce mounting and unmounting of your React components. import React from 'react'; import ReactDOM from 'react-dom'; export default

[React Fundamentals] Using Refs to Access Components

When you are using React components you need to be able to access specific references to individual components. This is done by defining a ref. Notice: 'ref' only works in class component, not in statless component. If we don't add "ref", three

[React Fundamentals] Owner Ownee Relationship

The owner-ownee relationship is used to designate a parent-child relationship with React components as it differs from the DOM relationship. import React from 'react'; export default class App extends React.Component { constructor(){ super(); //This

[React] React Fundamentals: Integrating Components with D3 and AngularJS

Since React is only interested in the V (view) of MVC, it plays well with other toolkits and frameworks. This includes AngularJS and D3. A app with React and D3.js: /** @jsx React.DOM */ var App = React.createClass({ getInitialState: function () { re

[React] React Fundamentals: with-addons - ReactLink

It can be tedious to type out all the boilerplate needed to get the DOM and states in React to synchronize. Luckily, React provides a version of the toolkit with a selection of available addons. This lesson is going to dig into ReactLink, and how thi