【发布时间】:2019-10-01 03:50:51
【问题描述】:
我有一个使用react-table 作为数据网格的函数。它最初是通过本地状态从父组件中的 Apollo 填充的,网格中的每一行都是数组中的对象。
当网格中的一个单元格发生变化时,整个行对象被写入状态。
我正在尝试使用 useEffect 触发将这些状态更改写回数据库的突变,但我正在努力解决两个主要问题:
- 突变没有写回数据库(虽然突变在 graphql 操场上确实有效)
- 了解如何仅将更改的行发送回突变。
主要功能(部分)
function Table2({ columns, data }) {
const [lines, setLines] = useState(data);
const [updateLine, {loading, error }] = useMutation(UPDATE_ITEM_MUTATION, {
variables:{ ...lines}
});
useEffect(() => {
updateLine
},[lines]);
const updateMyData = (rowIndex, columnID, value) => {
setLines(getLines =>
getLines.map((row, index) => {
if (index === rowIndex) {
console.log(row)
return {
...lines[rowIndex],
[columnID]: value
};
}
return row;
})
);
};
还有变异……
const UPDATE_ITEM_MUTATION = gql`
mutation UPDATE_LINE_MUTATION(
$id: ID!,
$description: String,
$project:Int
$category:Int
$account:Int
$amt:Int
$units:String
$multiple:Int
$rate:Int
){
updateLine(
where:{id: $id},
data: {
description: $description
project: $project
category: $category
account: $account
amt: $amt
units: $units
multiple: $multiple
rate: $rate
}) {
id
description
amt
}
}
`
如果您能给我一些建议,我将不胜感激。 谢谢
【问题讨论】:
-
line是什么样的? -
{id: "ck0pnyjp7fbox0b1728oqqnr5", amt: 1, description: "test4b", category: 3, __typename: "Line"}
标签: reactjs apollo react-apollo apollo-client