Skeanmy / Zero2One

学习笔记的仓库
0 stars 1 forks source link

【React】React基础 #8

Open Skeanmy opened 4 years ago

Skeanmy commented 4 years ago

React

组件间通信

父组件向子组件的通信

单向数据流的props

子组件向父组件通信

Ref

在典型的 React 数据流中,props是父组件与子组件交互的唯一方式。要修改一个子组件,你需要使用新的 props 来重新渲染它。但是,在某些情况下,你需要在典型数据流之外强制修改子组件。被修改的子组件可能是一个 React 组件的实例,也可能是一个 DOM 元素。

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return (
      <div ref={this.myRef} />
      <input ref={(ref) => {this.input = ref}}/>
    );
  }
}
Skeanmy commented 4 years ago

setState什么时候是同步的,什么时候是异步的?