React 还有这种操作方式

376 阅读1分钟

先定义一个组件MyComponent,在使用组件的地方调用

class MyComponent extends React.Componet{
  render(){
    return (
      <div>
      {
          this.props.children('李同钱你好帅');
      }
      </div>
    )
  }
}

使用组件时组件的children传递个方法!

<MyComponent> { (value)=>(<div>{value}</div>) } </MyComponent>

new Promise(resolve=>{
    resolve(1);
    Promise.resolve().then(()=>{console.log(2)});
    console.log(4);
}).then(t=>console.log(t));
console.log(3);

这个运行结果? 不懂的可以看看这个

通常我们会为事件处理程序传递额外的参数。例如,若是 id 是你要删除那一行的 id,以下两种方式都可以向事件处理程序传递参数:

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

上述两种方式是等价的,分别通过 arrow functionsFunction.prototype.bind 来为事件处理函数传递参数。