您当前的位置: 首页 > 

莉萝爱萝莉

暂无认证

  • 4浏览

    0关注

    58博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

React-组件

莉萝爱萝莉 发布时间:2021-05-04 23:05:17 ,浏览量:4

  1. 函数式组件
function MyComponent(){
      return 一个h2标签
    };
    ReactDOM.render(, document.getElementById('test'));
class MyClass {
  constructor(name) {
    this.name = name;
  }
}
const myClass = new MyClass("name");
console.log(myClass);
class MyChildClass extends MyClass {
  constructor(name, age) {
  	// 必须放在一开始的位置
    super(name);
    this.age = age;
  }
}
const myChildClass = new MyChildClass("name", "age");
console.log(myChildClass);
  1. 初始化state
constructor(props) {
    super(props);
    this.state = {
        isHot: true
    }
};
render() {
    console.log(this);
    return 今天天气{this.state.isHot ? "炎热" : "寒冷"}
}
  1. 点击变换
class MyComponent extends React.Component {
            constructor(props) {
                super(props);
                this.state = {
                    isHot: true
                };
                // 重定向this指针
                this.changeWeather = this.changeWeather.bind(this);
            };
            render() {
                return 今天天气{this.state.isHot ? "炎热" : "寒冷"}
            };
            changeWeather(){
            	// 用this.setState更改数值
                this.setState({
                    isHot: !this.state.isHot
                });
            }
        }
  1. props的使用
class MyComponent extends React.Component {
    render(){
        return (
            
                姓名:{this.props.name}
            
        )
    }
}

ReactDOM.render(, document.getElementById('test'));
  1. jsx的语法糖
MyComponent {
	// 设置默认类型
    static propTypes = {
    name: PropTypes.string,
    age: PropTypes.number.isRequired,
    speak: PropTypes.func
    }
    
    // 设置默认值
    static defaultProps = {
    name: '默认名称'
    }
};

const p = {age: 18};
ReactDOM.render(, document.getElementById('test'));
关注
打赏
1663903574
查看更多评论
立即登录/注册

微信扫码登录

0.0842s