Based on the code you've provided, there are a few potential areas where issues can arise. However, you haven't specified the exact error message or relevant context for the error you're encountering. Below, I'll outline some common issues and suggest improvements or debugging tips that may help you resolve your error.
Common Issues and Recommendations:
-
Improper Props Handling: In your DropDown component, you are not correctly using the setSelected prop to update the selected value. Instead, you're directly manipulating the store. You should use setSelected to notify the parent component about the change.
Fix example:
onChange={(e) => {
const newValue = Number(e.target.value);
store[title.toLowerCase()] = newValue; // updating store
setSelected(newValue); // updating parent state
}}
-
Type Safety: Ensure your selected prop can always be compared correctly against the values in your items array. Since selected is typed as string | number, you might need to consistently convert values to one type for comparison.
-
Use of toFixed in State Update: When you do .toFixed(2), it returns a string. However, you may want to store a number in your state. You might need to consider if you want the exact value (as a number) or a formatted string:
store[title.toLowerCase()] = parseFloat(Number(e.target.value).toFixed(2));
-
Uncontrolled Input: Using radio inputs that are checked based on selected means they are somewhat uncontrolled unless you pass through the selected values correctly. Confirm that selected has a matching type and value for the value attributes of your radio inputs.
-
Possible Invalid store State Reference: Ensure that the keys in the store used in the DropDown component match perfectly to avoid undefined references.
-
Console Errors: Check your developer console in the browser for specific error messages that can give insight into what might be going wrong.
Example Updated Code
Here’s a simplified and slightly modified version of your component with some of the suggestions applied:
import { proxy } from "valtio";
import React from "react";
// Store Definition
type Store = {
head: number;
neck: number;
shoulder: number;
arms: number;
chest: number;
waist: number;
hips: number;
legLength: number;
legWidth: number;
isMen: boolean;
height: string | number;
};
export const store = proxy<Store>({
head: 0,
neck: 0,
shoulder: 0,
arms: 0,
chest: 0,
waist: 0,
hips: 0,
legLength: 0,
legWidth: 0,
isMen: true,
height: 0.83,
});
// DropDown Component
type Props = {
title: string;
items: { name: string; value: string | number }[];
selected: number; // Ensure the selected has consistent type
setSelected: (value: number) => void;
};
const DropDown: React.FC<Props> = ({ items, title, selected, setSelected }) => {
return (
<div className="dropdown">
<h4>{title}</h4>
<details className="custom-select">
<summary className="radios">
{items.map(({ name, value }, i) => (
<React.Fragment key={i}>
<input
type="radio"
name={title.toLowerCase()}
value={value}
id={`${name.toLowerCase()}-${i}`}
title={name}
checked={selected === value}
onChange={(e) => {
const newValue = Number(e.target.value);
store[title.toLowerCase()] = newValue; // updating store
setSelected(newValue); // update selected in parent
}}
/>
</React.Fragment>
))}
</summary>
<ul className="list">
{items.map(({ name }, i) => (
<li key={i}>
<label htmlFor={`${name.toLowerCase()}-${i}`}>{name}</label>
</li>
))}
</ul>
</details>
</div>
);
};
export default DropDown;
Conclusion
Please run the updated code and check for console errors that can give more insight into what might be failing. Adjusting the handling of state values and ensuring proper radio input behavior are critical steps to getting this working correctly. If you still have issues, please provide the specific error message for more tailored help!