setState详解

2022-05-10  本文已影响0人  张_何

setState

两种写法

对象式写法

setState(stateChange, [callBack]) ----- 对象式的 setState
1.stateChange为状态改变对象
2.callback 是可选的回调函数,它在状态更新完毕、界面也更新后(render 调用后)才被调用,

this.setState({count:count+1},()=>{
  console.log(this.setState.count);
})
函数式写法

setState(updater, [callBack])
1.updater 为返回stateChange 对象的函数
2.updater 可以接受 state 和 props
3.callback 是可选的回调函数,它在状态更新、界面也更新后(render调用后)才被调用

this.setState((state,props)=>{
  return {count:state.count+1}
},()=>{
  console.log(this.setState.count);
})
两种方式的使用原则:
数据的合并
状态是集合的更新
 constructor(props) {
    super(props);
    this.state = {
      friends: [
        { name: "lilei", age: 20 },
        { name: "lily", age: 25 },
        { name: "lucy", age: 22 }
      ]
    }
  }
  insertData() {
    // 在开发中不要这样来做, 这样做,如果实现了shouldComponentUpdate,在方法里进行浅比较的话,认为两者是一样的,因为浅比较是比较两者的内存地址,
    // const newData = {name: "tom", age: 30}
    // this.state.friends.push(newData);
    // this.setState({
    //   friends: this.state.friends
    // });

    // 推荐做法
    const newFriends = [...this.state.friends];
    newFriends.push({ name: "tom", age: 30 });
    this.setState({
      friends: newFriends
    })
  }
  // 修改集合中某个对象的数据,也是要重新生成一个集合
  incrementAge(index) {
    const newFriends = [...this.state.friends];
    newFriends[index].age += 1;
    this.setState({
      friends: newFriends
    })
  }

setState 的更新

setState的同步
function changeText(){
  setTimeout(() => {
    this.setState({
      message:"你好啊,王小波"
    })
    console.log(this.state.message);// 你好啊,王小波
  }, 0);
}
componentDidMount(){
  const btnEl = document.getElementById("btn");
  btnEl.addEventListener('click',()=>{
    this.setState({
      message:"你好啊,王小波"
    });
    console.log(this.state.message);// 你好啊,王小波
  })
}
setState的异步
changeText(){
    // message 的数据之前设置的是 "你好啊,李银河"
    this.setState({
      message:"你好啊,王小波"
    })
    console.log(this.state.message);// 你好啊,李银河
}
如果获取异步的结果
changeText(){
    // message 的数据之前设置的是 "你好啊,李银河"
    this.setState({
      message:"你好啊,王小波"
    }, ()=>{
      console.log(this.state.message);// 你好啊,王小波
    })
    console.log(this.state.message);// 你好啊,李银河
}
setState 同步或异步的源码分析
const classComponentUpdater = {
  isMounted,
  enqueueSetState(inst, payload, callback) {
    const fiber = getInstance(inst);
    const currentTime = requestCurrentTimeForUpdate();
    const suspenseConfig = requestCurrentSuspenseConfig();
    const expirationTime = computeExpirationForFiber(
      currentTime,
      fiber,
      suspenseConfig,
    );

    const update = createUpdate(expirationTime, suspenseConfig);
    update.payload = payload;
    if (callback !== undefined && callback !== null) {
      if (__DEV__) {
        warnOnInvalidCallback(callback, 'setState');
      }
      update.callback = callback;
    }
    enqueueUpdate(fiber, update);
    scheduleWork(fiber, expirationTime);
  },
  ....
};
export function computeExpirationForFiber(
  currentTime: ExpirationTime,
  fiber: Fiber,
  suspenseConfig: null | SuspenseConfig,
): ExpirationTime {
  const mode = fiber.mode;
  if ((mode & BlockingMode) === NoMode) {
    return Sync;
  }

  const priorityLevel = getCurrentPriorityLevel();
  if ((mode & ConcurrentMode) === NoMode) {
    return priorityLevel === ImmediatePriority ? Sync : Batched;
  }

  if ((executionContext & RenderContext) !== NoContext) {
    // Use whatever time we're already rendering
    // TODO: Should there be a way to opt out, like with `runWithPriority`?
    return renderExpirationTime;
  }

  let expirationTime;
  if (suspenseConfig !== null) {
    // Compute an expiration time based on the Suspense timeout.
    expirationTime = computeSuspenseExpiration(
      currentTime,
      suspenseConfig.timeoutMs | 0 || LOW_PRIORITY_EXPIRATION,
    );
  } else {
    // Compute an expiration time based on the Scheduler priority.
    switch (priorityLevel) {
      case ImmediatePriority:
        expirationTime = Sync;
        break;
      case UserBlockingPriority:
        // TODO: Rename this to computeUserBlockingExpiration
        expirationTime = computeInteractiveExpiration(currentTime);
        break;
      case NormalPriority:
      case LowPriority: // TODO: Handle LowPriority
        // TODO: Rename this to... something better.
        expirationTime = computeAsyncExpiration(currentTime);
        break;
      case IdlePriority:
        expirationTime = Idle;
        break;
      default:
        invariant(false, 'Expected a valid priority level');
    }
  }
上一篇下一篇

猜你喜欢

热点阅读