版本号:
"antd": "^5.4.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
这是我的本地代码:
import React, {useEffect, useState} from 'react'
import ReactDOM from 'react-dom/client' //18
import {ConfigProvider, Transfer} from 'antd'
import type {TransferDirection} from 'antd/es/transfer'
interface RecordType {
key: string
title: string
description: string
chosen: boolean
}
const App = () => {
const [mockData, setMockData] = useState<RecordType[]>([])
const [targetKeys, setTargetKeys] = useState<string[]>([])
const [selectedKeys, setSelectedKeys] = useState<string[]>([])
const getMock = () => {
const tempTargetKeys = []
const tempMockData = []
for (let i = 0; i < 20; i++) {
const data = {
key: i.toString(),
title: `content${i + 1}`,
description: `description of content${i + 1}`,
chosen: i % 2 === 0
}
if (data.chosen) {
tempTargetKeys.push(data.key)
}
tempMockData.push(data)
}
setMockData(tempMockData)
setTargetKeys(tempTargetKeys)
}
useEffect(() => {
getMock()
}, [])
const handleChange = (
newTargetKeys: string[],
direction: TransferDirection,
moveKeys: string[]
) => {
console.log(newTargetKeys, direction, moveKeys)
setTargetKeys(newTargetKeys)
}
const handleSelectChange = (sourceSelectedKeys: string[], targetSelectedKeys: string[]) => {
setSelectedKeys([...sourceSelectedKeys, ...targetSelectedKeys])
}
const renderItem = (item: RecordType) => {
const customLabel = (
<span className='custom-item'>
{item.title} - {item.description}
</span>
)
return {
label: customLabel, // for displayed item
value: item.title // for title and filter matching
}
}
return (
<ConfigProvider>
<div>
<Transfer
dataSource={mockData}
listStyle={{
width: 300,
height: 300
}}
selectedKeys={selectedKeys}
targetKeys={targetKeys}
onChange={handleChange}
onSelectChange={handleSelectChange}
render={renderItem}
/>
</div>
</ConfigProvider>
)
}
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(<App />)
// render(<App />, document.getElementById('root'))
这个是报错截图:
只是把官网的第一个例子用在本地上(直接复制粘贴都没做任何修改),当点击全选按钮的时候就报这个警告错误:在渲染另一个组件(' Transfer ')时无法更新一个组件(' App ')。在' Transfer '中查找错误的setState()调用。
经过我自己测试,发现是 onSelectChange 方法中的 setSelectedKeys 造成的,只要注释这一句就不会报错了;但是这是怎么回事呢??
有没有遇到一样问题的,帮忙看看怎么回事???
你可以用 useSyncExternalStore Hook
import { useSyncExternalStore } from 'use-sync-external-store';
// ...
const App = () => {
const [mockData, setMockData] = useSyncExternalStore(() => [], []);
const [targetKeys, setTargetKeys] = useSyncExternalStore(() => [], []);
const [selectedKeys, setSelectedKeys] = useSyncExternalStore(() => [], []);
// ...
};
我试着在点击元素HomeNav下的导航栏中的第二个li时显示警报。您可以在codepen上签出完整的代码 HTML
要求,pc端官网项目,纯静态的html,期望加一些css3 动画特效。请问选择jq合适吗?有更好的推荐吗?
当简单报表不适用于子报表时,此操作有效...
element ui 树状表格选择父节点子节点全选,子节点不全选父节点半选? el-table没有相关示例,需要手动实现好像?
点击当前勾选框check_id中值被替换成当前选中的值,但怎么取消上一个的选中状态
我知道这可能是一个容易修复的错误,但我没有找到它。当警报框出现并点击时,需要点击2次才能关闭它