Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onChange 用useCallback 包裹 ,onChange里面无法修改父组件的状态!把useCallback 干掉可以吗? #111

Closed
RobinYang11 opened this issue Apr 11, 2023 · 10 comments

Comments

@RobinYang11
Copy link

image
image

@jaywcjlove
Copy link
Member

@RobinYang11
Copy link
Author

import "./styles.css";
import { useState } from "react";
import Alpha from "@uiw/react-color-alpha";
import { hsvaToRgbaString } from "@uiw/color-convert";

function Demo() {
  const [hsva, setHsva] = useState({ h: 0, s: 0, v: 68, a: 1 });
  const [obj,setObj] = useState({
    name:"robin",
    age:33
  })

  return (
    <>
      <Alpha
        hsva={hsva}
        onChange={(newAlpha) => {
          setHsva({ ...hsva, ...newAlpha });
          setObj({
            ...obj
          })
        }}
      />
      {obj.name}
      <button onClick={()=>{
        setObj({
          ...obj,
          name: "changedName"
        })
      }}>changeName</button>
      <div
        style={{
          background: hsvaToRgbaString(hsva),
          marginTop: 30,
          padding: 10
        }}
      >
        {JSON.stringify(hsva)}
    
      </div>
    </>
  );
}

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Demo />
    </div>
  );
}

@RobinYang11
Copy link
Author

先点击button 改变名字, 然后你再拖动 ,name 就变为初始值了,就是因为包了useCallback

@jaywcjlove
Copy link
Member

image

@RobinYang11 你拖动的时候,不是去改变了 name ?

@jaywcjlove
Copy link
Member

先点击button 改变名字, 然后你再拖动 ,name 就变为初始值了,就是因为包了useCallback

我看了示例,name 并没有变成初始值,而是 onChange 去更改了 name 的值

@RobinYang11
Copy link
Author

你再刷新一下 我的例子 我改了,我增加了一个数组字段

@RobinYang11
Copy link
Author

@jaywcjlove
Copy link
Member

@RobinYang11 Upgrade v1.1.4

github-actions bot pushed a commit that referenced this issue Apr 12, 2023
@jaywcjlove
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants