如何在React中将元素的ID保存在状态中

huangapple go评论70阅读模式
英文:

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 元素上,你可以找到 idvalue

英文:

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 = () =&gt; {

    const [value, setValue] = React.useState(&#39;&#39;);
    const [id, setId] = React.useState(&#39;&#39;);

    const handleChange = (event) =&gt; {
        let element = event.target;
        let options = element.options;
        let { id, value } = options[options.selectedIndex];
 
        setValue(value);
        setId(id);
        
        console.log(`Change: ${value} (${id})`);
    };

    return (
        &lt;select id={id} value={value} onChange={handleChange}&gt;
             &lt;option id={1} value={&#39;foo&#39;}&gt;{&#39;Foo&#39;}&lt;/option&gt;
             &lt;option id={2} value={&#39;bar&#39;}&gt;{&#39;Bar&#39;}&lt;/option&gt;
        &lt;/select&gt;
    )
}
ReactDOM.render(&lt;Example /&gt;, document.getElementById(&quot;react&quot;));

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;react&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月23日 21:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定