使用Label时出现‘Error: Converting circular structure to JSON’错误求助
Hey there! Let's break down why you're seeing this error and how to fix it quickly.
What's Causing the Error?
That "circular structure" error pops up when you try to serialize an object that references itself in a loop. In your code, the culprit is this line:
console.log(options)
The event.target.options is an HTMLCollection—a live DOM collection where each <option> element has a reference back to its parent <select>, and the <select> has a reference to all its <option>s. This creates a circular loop, and when your environment (like React DevTools or browser console) tries to convert this collection to JSON for display, it chokes.
The Fix
You have two simple options here:
Remove the problematic log
If you don't need to debug the options collection, just delete theconsole.log(options)line entirely. Your form logic (straight from the React docs) is perfectly valid, so this will make the error disappear immediately.Log only the data you need
If you do want to inspect option details, extract specific values instead of logging the entire collection. For example:const { options } = event.target; // Get the text of the selected option const selectedOption = Array.from(options).find(opt => opt.selected); console.log('Selected option text:', selectedOption?.text); // Or just log the already-selected value (which you're already using) console.log('Selected value:', event.target.value);Using
Array.fromconverts the live HTMLCollection into a plain array, and extracting specific properties avoids the circular reference issue.
Quick Note
Your core form code is exactly how React recommends handling controlled select inputs—this error has nothing to do with your Label component or form logic itself. It's just a tiny debugging misstep with DOM collections!
内容的提问来源于stack exchange,提问作者Colin Burke

