为什么传播下一个对象是清空仅存在于第一个对象中的数组属性字段?

发布于 2025-02-01 20:05:50 字数 1676 浏览 3 评论 0原文

const initialState = {
  title: '',
  description: '',
  price: '',
  categories: [],
  category: '',
  subCategoriesList: [],
  subs: [],
  shipping: '',
  quantity: '',
  images: [],
  colors: ['Black', 'Brown', 'Silver', 'White', 'Blue'],
  brands: ['Apple', 'Samsung', 'Microsoft', 'Lenovo', 'ASUS', 'Dell'],
  color: '',
  brand: '',
}

const ProductUpdate = () => {
  const [values, setValues] = useState(initialState)
  const user = useSelector((state) => state.user)
  const authToken = user?.token

  const router = useRouter()

  const {
    query: { slug },
  } = router

  useEffect(() => {
  // with this loadCategories function, values.categories array get filled
    loadCategories()
    if (!slug && !values.categories) {
      return
    }
  //as soon as loadProductInfo happens, values.categories array becomes empty
    loadProductInfo(slug)
  }, [slug])



  const loadCategories = async () => {
    const response = await getCategories()
    setValues({ ...values, categories: response.data })
  }

 
  // this function makes values.categories empty array
  const loadProductInfo = async (slug) => {
    const response = await getProductInfo(slug)
    console.log('Product info', response.data) 
  // response.data doesn't have fields named categories, but still it makes categories array 
   empty
    setValues({ ...values, ...response.data })
  }

  console.log('Categories see here', values.categories)

来自loadProductInfo的响应data不包含任何名为类别的字段。在LoadCategories()完成并从响应中设置值之后,我看到类别数组字段被填充,但是在LoadProductInfo()完成后,它再次使该类别阵列array Field空了,奇怪的是whects.data。有任何字段命名类别,那么为什么要制作值。类别数组字段再次为空?这里有什么与我不了解的声明有关的吗?

const initialState = {
  title: '',
  description: '',
  price: '',
  categories: [],
  category: '',
  subCategoriesList: [],
  subs: [],
  shipping: '',
  quantity: '',
  images: [],
  colors: ['Black', 'Brown', 'Silver', 'White', 'Blue'],
  brands: ['Apple', 'Samsung', 'Microsoft', 'Lenovo', 'ASUS', 'Dell'],
  color: '',
  brand: '',
}

const ProductUpdate = () => {
  const [values, setValues] = useState(initialState)
  const user = useSelector((state) => state.user)
  const authToken = user?.token

  const router = useRouter()

  const {
    query: { slug },
  } = router

  useEffect(() => {
  // with this loadCategories function, values.categories array get filled
    loadCategories()
    if (!slug && !values.categories) {
      return
    }
  //as soon as loadProductInfo happens, values.categories array becomes empty
    loadProductInfo(slug)
  }, [slug])



  const loadCategories = async () => {
    const response = await getCategories()
    setValues({ ...values, categories: response.data })
  }

 
  // this function makes values.categories empty array
  const loadProductInfo = async (slug) => {
    const response = await getProductInfo(slug)
    console.log('Product info', response.data) 
  // response.data doesn't have fields named categories, but still it makes categories array 
   empty
    setValues({ ...values, ...response.data })
  }

  console.log('Categories see here', values.categories)

The response.data that is coming from loadProductInfo doesn't contain any field named categories. After loadCategories() finishes and sets the value from response, I see that categories array field gets filled, but after loadProductInfo() finishes, it again makes that categories array field empty, it's strange that the response.data coming from it doesn't have any fields named categories, so why is it making values.categories array field empty again? Is anything here related to state that I am not understanding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2025-02-08 20:05:50

我建议您将固定器与oldvalues一起使用,也许您的两个API调用之间存在冲突...

setValues(oldValues => ({ ...oldValues, ...response.data }))

I suggest you use the setters with the oldValues, maybe is there a conflict between your two api calls...

setValues(oldValues => ({ ...oldValues, ...response.data }))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文