angular对照react

261 阅读1分钟

1. Angular模板 vs JSX

AngularReacthook
属性绑定[class]="className"className={this.className}
关键字class写为className
关键字for写为htmlFor
事件绑定(click)="handleClick()"onClick={this.handleClick}
1. 绑定函数指针,避免渲染组件时触发子组件事件流
2.使用箭头函数,避免this指向问题
组件属性@Input()xxx只读属性this.props.xxx
propTypes定义类型
组件方法@Output()xxxthis.props.xxx方法
内容投影<ng-content>关键字this.props.children
使用React.Children处理子节点undefined/object/array情况
获取真实DOMelementRefref属性
React.createRef()
组件状态直接声明组件类属性state
getInitialState设置初始状态
setState方法修改状态值,修改之后会调用render方法重新渲染DOM
const [xxx, setStatexxx] = useState('initial value');
组件生命周期xcomponentWillMount()
componentDidMount()
componentWillUpdate(object nextProps, object nextState)
componentDidUpdate(object prevProps, object prevState)
componentWillUnmount()
componentWillReceiveProps(object nextProps):已加载组件收到新的参数时调用
shouldComponentUpdate(object nextProps, object nextState):组件判断是否重新渲染时调用