React 组件 API 简单演示

前端开发 作者: 2024-08-19 21:10:02
设置状态:setState替换状态:replaceState设置属性:setProps替换属性:replaceProps强制更新:forceUpdate获取DOM节点:findDOMNode判断组件挂
import React,{ Component,Fragment } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import PropTypes from 'prop-types';

class Clock extends Component{
  constructor(props){
    super(props);
    this.state={
      countNum:0
    }
    this.addCount=this.addCount.bind(this);
  }

  addCount(){
    .setState({
      countNum:this.state.countNum+1
    })
  }

  render(){
    return(
      <Fragment>
        <button onClick={this.addCount}>点击计数</button>
        <p>{this.state.countNum}</p>
      </Fragment>
    )
  }
}

  ReactDOM.render(
    <div>
       <Clock />  
    </div>,document.getElementById('example')
  );

serviceWorker.unregister();
this.state.comment = 'Hello';
this.setState({comment: 'Hello'});
// Wrong
.setState({
  counter: this.state.counter + .props.increment,});
 Correct
this.setState((prevState,props) => ({
  counter: prevState.counter + props.increment
}));
constructor(props) {
    super(props);
    this.state = {
      posts: [],comments: []
    };
}
componentDidMount() {
    fetchPosts().then(response => {
      .setState({
        posts: response.posts
      });
    });

    fetchComments().then(response =>.setState({
        comments: response.comments
      });
    });
}
componentDidMount() {
    this.mounted = true;
}

componentWillUnmount() {
    false;
}

总结

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65146.html