In my react app, I'm setting multiple values in an object. I also have some input field with a button/icon attached to it. What I want is, to get the id
value of that button on click and with that id
value I want to check if the object has the same property or not. If it has that property I want to use it later for separate functionality. But right now I'm just console logging it. For this I've attached a handleClick()
handler and getting the e.target.id
and printing it. But there are 2 problems I'm facing right now.
- Sometimes I'm getting
empty string
on clicking. Sometimes I've to click multiple times to get the value. - In the
handleClick()
I'm not getting the data from the object. I've tried usinghasOwnProperty
orkey in Object
, but they are not working
code
const dropdowns = {
group: [
{ code: "10202", description: "10202 - 10202 - 10202 - 10202 - 10202" },
{ code: "10202", description: "10202 - 10202 - 10202 - 10202 - 10202" },
{ code: "10202", description: "10202 - 10202 - 10202 - 10202 - 10202" },
{ code: "10202", description: "10202 - 10202 - 10202 - 10202 - 10202" },
{ code: "10202", description: "10202 - 10202 - 10202 - 10202 - 10202" },
{ code: "10202", description: "10202 - 10202 - 10202 - 10202 - 10202" },
],
supplier: [
{ code: "3039", name: "3039 - 3039 - 3039 - 3039 - 3039" },
{ code: "3039", name: "3039 - 3039 - 3039 - 3039 - 3039" },
{ code: "3039", name: "3039 - 3039 - 3039 - 3039 - 3039" },
{ code: "3039", name: "3039 - 3039 - 3039 - 3039 - 3039" },
{ code: "3039", name: "3039 - 3039 - 3039 - 3039 - 3039" },
{ code: "3039", name: "3039 - 3039 - 3039 - 3039 - 3039" },
],
attribute: [
{ code: "274", name: "274 - 274 - 274 - 274 - 274" },
{ code: "274", name: "274 - 274 - 274 - 274 - 274" },
{ code: "274", name: "274 - 274 - 274 - 274 - 274" },
{ code: "274", name: "274 - 274 - 274 - 274 - 274" },
{ code: "274", name: "274 - 274 - 274 - 274 - 274" },
{ code: "274", name: "274 - 274 - 274 - 274 - 274" },
],
unit: [
{ code: "EA", name: "EA - EA - EA - EA - EA" },
{ code: "EA", name: "EA - EA - EA - EA - EA" },
{ code: "EA", name: "EA - EA - EA - EA - EA" },
{ code: "EA", name: "EA - EA - EA - EA - EA" },
{ code: "EA", name: "EA - EA - EA - EA - EA" },
{ code: "EA", name: "EA - EA - EA - EA - EA" },
],
uses: [
{ code: "1", name: "1 - 1 - 1 - 1 - 1" },
{ code: "1", name: "1 - 1 - 1 - 1 - 1" },
{ code: "1", name: "1 - 1 - 1 - 1 - 1" },
{ code: "1", name: "1 - 1 - 1 - 1 - 1" },
{ code: "1", name: "1 - 1 - 1 - 1 - 1" },
{ code: "1", name: "1 - 1 - 1 - 1 - 1" },
],
};
const SetupBar = () => {
const [formData, setFormData] = useState({
group: "",
supplier: "",
attribute: "",
unit: "",
uses: "",
});
const handleClick = (value) => {
console.log(value);
// console.log(dropdowns.value); I want this
};
return (
<div className="parent">
<div className="input-fields">
<label htmlFor="attribute">Attribute ID</label>
<div className="input-grp">
<input type="text" />
<CgArrowRightR
id="attribute"
style={{ fontSize: "21px" }}
onClick={(e) => handleClick(e.target.id)}
/>
</div>
</div>
<div className="input-fields">
<label htmlFor="color">Color</label>
<input type="text" id="color" />
</div>
<div className="input-fields">
<label htmlFor="group">Group Code</label>
<div className="input-grp">
<input type="text" />
<CgArrowRightR
id="group"
style={{ fontSize: "21px" }}
onClick={(e) => handleClick(e.target.id)}
/>
</div>
</div>
<div className="input-fields">
<label htmlFor="supplier">Supplier</label>
<div className="input-grp">
<input type="text" />
<CgArrowRightR
id="supplier"
style={{ fontSize: "21px" }}
onClick={(e) => handleClick(e.target.id)}
/>
</div>
</div>