英文:
How to save the id of an element in a state in react
问题
好的,以下是您要翻译的内容:
"Good morning, I have a question for you. I am learning react and I have a question that I cannot solve.
I'm mapping a json and rendering the elements in an option inside a select. Every time the value of the select is changed I need to save the value and the id. I save the value by setting a state like event.target.value but I don't know how to save the id, I tried to do something similar as I did with the value but it didn't work.
I'm sorry if the question is basic, I wouldn't be finding answers, in advance, thank you very much
const [value, setValue] = React.useState('');
const [id, setId] = React.useState('');
const handleChange = (event) => {
setValue(event.target.value);
setId(event.target.id)
};
"
英文:
Good morning, I have a question for you. I am learning react and I have a question that I cannot solve.
I'm mapping a json and rendering the elements in an option inside a select. Every time the value of the select is changed I need to save the value and the id. I save the value by setting a state like event.target.value but I don't know how to save the id, I tried to do something similar as I did with the value but it didn't work.
I'm sorry if the question is basic, I wouldn't be finding answers, in advance, thank you very much
const [value, setValue] = React.useState('');
const [id, setId] = React.useState('');
const handleChange = (event) => {
setValue(event.target.value);
setId(event.target.id)
};
<select id={id} value={value} onChange={handleChange}>
{hologram.map((option) => (
<option id={option.id} value={option.path}>{option.denomination}</option>
))}
</select>
答案1
得分: 1
你需要使用 event.target
获取 element
,
然后使用 element.options.selectedIndex
获取选定的索引。
要获取原始选项,使用 element.options[element.options.selectedIndex]
,在这个 DOM 元素上,你可以找到 id
和 value
。
英文:
You need to use event.target
to get the element
Then use element.options.seletedIndex
to get the selected index.
To get the original option, use element.options[element.options.seletedIndex]
, on this DOM element, you can find the id
and value
Small demo:
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const { useState } = React;
const Example = () => {
const [value, setValue] = React.useState('');
const [id, setId] = React.useState('');
const handleChange = (event) => {
let element = event.target;
let options = element.options;
let { id, value } = options[options.selectedIndex];
setValue(value);
setId(id);
console.log(`Change: ${value} (${id})`);
};
return (
<select id={id} value={value} onChange={handleChange}>
<option id={1} value={'foo'}>{'Foo'}</option>
<option id={2} value={'bar'}>{'Bar'}</option>
</select>
)
}
ReactDOM.render(<Example />, document.getElementById("react"));
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论