ag-grid中row-id=[object Object]重复节点ID问题求助
Hey there! Let's sort out that frustrating duplicate node ID warning you're seeing in ag-Grid. The root cause here is simple: your getRowNodeId callback is returning an object instead of a unique primitive value (like a string or number). When ag-Grid tries to use this object as a row ID, it gets converted to the string [object Object]—and since multiple rows end up with this same ID, you get the duplicate warning.
Here's how to fix it step by step:
1. Correct your getRowNodeId implementation
First, check what your callback is returning. If you're returning an object (like in the example below), that's the problem.
❌ Bad implementation (returns an object):
getRowNodeId: (data) => { // This object gets coerced to "[object Object]" return { id: data.id }; }
✅ Good implementation (returns a unique primitive):
getRowNodeId: (data) => { // Return a unique string or number from your row data return data.id; }
2. Ensure your row IDs are truly unique
Even if you're returning a primitive value, if multiple rows share the same ID, you'll still get the warning. If your single data field isn't unique, use a composite key to create a unique identifier:
// Combine multiple fields to make a unique ID getRowNodeId: (data) => { return `${data.userId}-${data.orderId}`; }
3. Handle missing or null IDs
If some rows don't have an ID field (or it's null/undefined), add a fallback to generate a unique temporary ID:
getRowNodeId: (data) => { // Use the row's ID if available, else generate a random unique string return data.id || `row-${Math.random().toString(36).slice(2, 10)}`; }
4. Debug your callback's output
To double-check what your callback is returning, add a console.log to verify:
getRowNodeId: (data) => { const rowId = data.id; console.log(`Row data:`, data, `→ Row ID:`, rowId); return rowId; }
Look for rows where rowId is an object, or where multiple rows have the same rowId value.
Final Notes
The key takeaway is that getRowNodeId must return a unique, non-object value for each row. Once you fix that, ag-Grid will be able to properly identify each row, and the duplicate ID warning should disappear.
内容的提问来源于stack exchange,提问作者Riddhi verma

