hooks useState更新对象
点击2个按钮,更新一个state对象,互不影响

代码:
import React, { useState } from 'react';
export default () => {
  const [state, setState] = useState({
    count: 0,
    count2: 0,
    name: 'aaa',
  });
  const click = () => {
    setState({
      ...state,
      count: state.count + 1,
    });
  };
  const change = () => {
    setState({
      ...state,
      name: 'bbb',
    });
  };
  return (
    <div>
      <div>
        count:{state.count},name:{state.name}
      </div>
      <button onClick={click}>+1</button>
      <button onClick={change}>name</button>
    </div>
  );
};
博客园作者:herry菌朋友,看到这里,关注作者的公众号吧,不漏掉更新哦

 
   
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号