作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个codesandbox for the problem .
该示例分为三个部分:
一个 useEffect 实例化网格,而另一个 useEffect 监听状态更改以更新网格。
一切正常,直到 row is clicked in the grid library .
我收到此错误。
TypeError: Cannot read property 'companyName' of undefined
471 | id={"input-companyName"}
472 | list={"options-companyName"}
473 | className={"form-input"}
> 474 | value={formJournalItems.companyName}
475 | onChange={handleDatalistChange}
476 | onKeyUp={handleKeyUp}
477 | ref={refCompanyName}
325 |
326 | function handleTableRowClick(journalItemId) {
327 | let journalItem = journalItems.filter(item => item.id === journalItemId)[0]
> 328 | setFormJournalItems(journalItem)
329 | }
330 |
331 | function isValidFormInputs() {
这是有问题的代码,其余的可以在 codesandbox 中看到.
let refCompanyName = React.createRef();
let refTable = useRef(null);
let table = useRef(null);
const [journalItems, setJournalItems] = useState([]);
const initialFormJournalItems = {
id: "",
journalId: "",
companyId: "",
companyName: "",
documentKey: "",
documentDate: "",
debitAccountId: "",
debitAccount: "",
debit: "",
creditAccountId: "",
creditAccount: "",
credit: ""
}
const [formJournalItems, setFormJournalItems] = useState(initialFormJournalItems);
useEffect(() => {
fetch(`http://localhost:4000/journals/${props.match.params.key}/items`)
.then(res => res.json())
.then(data => {
setJournalItems(data)
})
.catch(err => err);
table.current = new Tabulator(refTable.current, {
data: journalItems,
height: "100%",
layout: "fitColumns",
rowClick: function (e, row) {
//e - the click event object
//row - row component
console.log("tabulator journalitems", journalItems) // <------ EMPTY []
console.log(row._row.data)
handleTableRowClick(row._row.data.id)
},
columns: [
{ title: "Компанија", field: "companyName" },
{ title: "Документ", field: "documentKey" },
{ title: "Датум", field: "documentDate" },
{ title: "Должи", field: "debitAccount" },
{ title: "Износ", field: "debit" },
{ title: "Побарува", field: "creditAccount" },
{ title: "Износ", field: "credit" },
],
});
}, []);
// Updates the tabulator table on item change
useEffect(() => {
console.log("useEffect journalItems", journalItems) // <------ POPULATED
console.log("useEffect refTable", refTable)
console.log("useEffect table", table)
table.current.replaceData(journalItems)
}, [journalItems]);
function handleTableRowClick(journalItemId) {
let journalItem = journalItems.filter(item => item.id === journalItemId)[0]
setFormJournalItems(journalItem)
}
return (
<div>
<input
type={"text"}
name={"companyName"}
id={"input-companyName"}
list={"options-companyName"}
className={"form-input"}
value={formJournalItems.companyName}
onChange={handleDatalistChange}
onKeyUp={handleKeyUp}
ref={refCompanyName}
multiple
/>
</div>
)
最佳答案
我设法使用函数变量的 useRef 并通过将函数定义移动到 useEffect 内来解决这个问题?
const [journalItems, setJournalItems] = useState([]);
let handleTableRowClick = useRef(null);
useEffect(() => {
fetch(`http://localhost:4000/journals/${props.match.params.key}/items`)
.then(res => res.json())
.then(data => {
setJournalItems(data) // Sets the state when the AJAX completes
})
.catch(err => err);
table.current = new Tabulator(refTable.current, {
rowClick: function (e, row) {
console.log("tabulator journalitems", journalItems) // State is lost returns []
handleTableRowClick.current(row._row.data.id)
},
columns: [
{ title: "Компанија", field: "companyName" },
{ title: "Документ", field: "documentKey" },
],
});
}, []);
useEffect(() => {
console.log("useEffect journalItems", journalItems) // Works fine
table.current.replaceData(journalItems) // Uses the new state
handleTableRowClick.current = (journalItemId) => {
console.log("handletablerowclick journalItems", journalItems)
// Find the clicked row from all the rows
let journalItem = journalItems.filter(item => item.id === journalItemId)[0]
setFormJournalItems(journalItem)
}
}, [journalItems]);
关于javascript - 类型错误 : Cannot read property ' ' of undefined via useEffect and Tabulator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60340283/
我是一名优秀的程序员,十分优秀!