gpt4 book ai didi

reactjs - 为什么当状态更新时渲染中的变化没有更新?

转载 作者:行者123 更新时间:2023-12-03 12:39:13 24 4
gpt4 key购买 nike

我创建了一个 Hook 来访问数据库及其方法的集合。

import { remote } from 'electron'
import { useState, useEffect } from "react"

function useCollections(collections = []) {

let [dbInstances, setDbInstances] = useState(null)
let [data, setData] = useState(null)

// DB METHODS

// Create
let create = async (doc, dbName) => {
await dbInstances[dbName].create(doc)
let newData = await dbInstances[dbName].readAll()
setData({ ...data, [dbName]: newData })
}

// Get details
let getDetails = async (id, dbName) => {
let doc = await dbInstances[dbName].read(id)
return doc
}

// Delete
let deleteOne = async (id, dbName) => {
await dbInstances[dbName].deleteOne(id)
let newData = await dbInstances[dbName].readAll()
setData({ ...data, [dbName]: newData })
}

// Update
let updateOne = async (id, updatedDoc, dbName) => {
await dbInstances[dbName].archive(id, updatedDoc)
let newData = await dbInstances[dbName].readAll()
setData({ ...data, [dbName]: newData })
}

// EFFECTS

useEffect(() => {
console.log('mounting component')
let newDBIs = {}
collections.forEach(col => newDBIs[col] = remote.getGlobal(col))
console.log('db instances settted', newDBIs)
setDbInstances(newDBIs)
}, [])

// When DBs are instantiated, request all docs and set data with response
useEffect(() => {
if (
dbInstances !== null &&
data === null &&
Object.keys(dbInstances).length === collections.length)
{
console.log('setting data')
let newData = {}
collections.forEach(async col => newData[col] = await dbInstances[col].readAll())
console.log('data setted => ', newData)
setData(newData)
}
}, [dbInstances])

return {
data,
create,
getDetails,
deleteOne,
updateOne
};
}

export default useCollections;
在使用钩子(Hook)返回的数据的组件中,即使变量 data包含预期的数据,这些不会呈现。
import WindowsLayout from "../../components/layout/WindowsLayout"
import { useState, useEffect } from "react"
import { remote } from "electron"
import useCollections from "../../hooks/useCollections"

const EditWorkWindow = ({ workId }) => {

let { data, deleteOne, updateOne } = useCollections([
'workDB',
'studioDB',
'rateDB'
])

useEffect(() => {
if (data !== null) console.log(data)
}, [data])


return (
<WindowsLayout title="Edit work window">
<div style={{ height: 243 }} className="window-content">
<div className="padded-more bg-gray-200">
<h2>{JSON.stringify(data)}</h2>
<button onClick={() => console.log(data)}>CLG</button>
</div>
</div>
</WindowsLayout >
)
}

export default EditWorkWindow

效果 Hook 通过控制台显示预期数据。 <h2>{JSON.stringify(data)}</h2> = {}单击按钮时,预期的数据将显示在控制台上。
我不明白为什么如果数据包含属性,它们不会显示在 {JSON.stringify(data)} 中。
这是单击按钮后控制台显示的内容
console.log(data) image
这是示例 data及其属性
{
"workDB":[
{
"product":"Work name 1",
"amounts_rates":[
{
"rate":"EflcQflqu2oWWVk2",
"amount":6
},
{
"rate":"FeMIX00pwpmZwoVW",
"amount":1
}
],
"date":"2020-08-31",
"studio":"BCvPeWzMiS8fZsmS",
"_id":"2ZvHMWFODBHYWEBo",
"createdAt":"2020-08-31T09:39:21.077Z",
"updatedAt":"2020-08-31T09:39:21.077Z"
},
{
"product":"Work name 2",
"amounts_rates":[

],
"date":"2020-09-02",
"director":"",
"_id":"PRpp1OQcJnkFKeR0",
"createdAt":"2020-09-01T19:56:33.201Z",
"updatedAt":"2020-09-01T19:56:33.201Z"
}
],
"studioDB":[
{
"name":"Studio name 1",
"_id":"0J1AVXtgDjwBjRS9",
"createdAt":"2020-08-25T10:18:40.004Z",
"updatedAt":"2020-08-25T10:18:40.004Z"
},
{
"name":"Studio name 2",
"_id":"8sFH7gncaM6V7lHh",
"createdAt":"2020-08-25T10:19:45.232Z",
"updatedAt":"2020-08-25T10:19:45.232Z"
}
],
"rateDB":[
{
"name":"Rate name 1",
"value":4.1,
"_id":"EflcQflqu2oWWVk2",
"createdAt":"2020-08-25T10:24:17.357Z",
"updatedAt":"2020-08-25T10:24:17.357Z"
},
{
"name":"Rate name 1",
"value":34,
"_id":"FeMIX00pwpmZwoVW",
"createdAt":"2020-08-25T10:24:25.628Z",
"updatedAt":"2020-08-25T10:24:25.628Z"
}
]
}

最佳答案

它是异步问题。

// When DBs are instantiated, request all docs and set data with response
useEffect(() => {
if (
dbInstances !== null &&
data === null &&
Object.keys(dbInstances).length === collections.length)
{
console.log('setting data')
let newData = {}
collections.forEach(async col => newData[col] = await dbInstances[col].readAll())
console.log('data setted => ', newData)

setData(newData) // <-- 👋LOOK HERE

}
}, [dbInstances])
所以你 let newData = {}空对象,并通过调用 setData() 将其发送出去以触发更新重新渲染, 但是 newData调用时为空。
在你的渲染函数中 JSON.stringify(data)拾取数据,但在渲染时它仍然是空的!
只有当 async col => newData[col] = await someValue通话已解决,您的 newData房产 将获得新的值, newData对象保持不变。但是当它解决时,渲染已经完成。
解决方法:等到异步函数调用解决后,再调用 setData()
useEffect(() => {
// ...
const promises = collections.map(async col => {
newData[col] = await dbInstances[col].readAll())
})
Promise.all(promises).then(() => { setData(newData) })
})
在控制台中检查时看到更新值的原因是因为您没有“足够快”地检查。当您在控制台中单击鼠标展开对象时,它的属性已经被赋值。但如果你改变
console.log('data setted => ', newData) 
// to
console.log('data setted => ', JSON.stringify(newData))
你会看到一个空的对象。

关于reactjs - 为什么当状态更新时渲染中的变化没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63722172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com