React相同组件的多个实例得到了相同的回调

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

React multiple instances of same component are getting the same callback

问题

以下是您提供的代码的翻译:

import React, { useState } from 'react';
import styles from './checkbox.module.css';

interface CheckboxProps {
  initialState: boolean;
  onToggle: () => void;
}

const Checkbox: React.FC<CheckboxProps> = ({ initialState, onToggle }) => {
  const [checked, setChecked] = useState<boolean>(initialState);

  const handleChange = () => {
    const newChecked = !checked;
    setChecked(newChecked);
    onToggle();
  };

  return (
    <>
      <input
        className={styles.switch}
        type="checkbox"
        id="switch"
        checked={checked}
        onChange={handleChange}
      />
      <label className={styles['switch-label']} htmlFor="switch">Toggle</label>
    </>
  );
};

export default Checkbox;

CSS 代码:

.switch {
  height: 0;
  width: 0;
  visibility: hidden;
}

.switch-label {
  cursor: pointer;
  text-indent: -9999px;
  width: 51.2px; 
  height: 25.6px; 
  background: rgb(241, 44, 44);
  display: block;
  border-radius: 80px;
  position: relative;
}

.switch-label:after {
  content: "";
  position: absolute;
  top: 4px; 
  left: 4px; 
  width: 17.6px; 
  height: 17.6px; 
  background: #fff;
  border-radius: 70.4px; 
  transition: 0.3s;
}

.switch:checked + .switch-label {
  background: #a5ce1e;
}

.switch:checked + .switch-label:after {
  left: calc(100% - 4px); 
  transform: translateX(-100%);
}

.switch-label:active:after {
  width: 22.4px; 
}

如果您像下面这样使用组件,React 只会执行第一个组件的回调,即使您点击第二个或第三个组件也是如此:

<div>
<Checkbox key={1} initialState={false} onToggle={() => { console.log("One") }} />
<Checkbox key={2} initialState={false} onToggle={() => { console.log("Two") }} />
<Checkbox key={3} initialState={false} onToggle={() => { console.log("Three") }} />
</div>
英文:

I have a React Component that is only executing the first callback of the first Component instance, even if I click the second or third component. Here is my code:

import React, { useState } from &#39;react&#39;;
import styles from &#39;./checkbox.module.css&#39;;

interface CheckboxProps {
  initialState: boolean;
  onToggle: () =&gt; void;
}

const Checkbox: React.FC&lt;CheckboxProps&gt; = ({ initialState, onToggle }) =&gt; {
  const [checked, setChecked] = useState&lt;boolean&gt;(initialState);

  const handleChange = () =&gt; {
    const newChecked = !checked;
    setChecked(newChecked);
    onToggle();
  };

  return (
    &lt;&gt;
      &lt;input
        className={styles.switch}
        type=&quot;checkbox&quot;
        id=&quot;switch&quot;
        checked={checked}
        onChange={handleChange}
      /&gt;
      &lt;label className={styles[&#39;switch-label&#39;]} htmlFor=&quot;switch&quot;&gt;Toggle&lt;/label&gt;
    &lt;/&gt;
  );
};

export default Checkbox;

CSS code:

.switch {
  height: 0;
  width: 0;
  visibility: hidden;
}

.switch-label {
  cursor: pointer;
  text-indent: -9999px;
  width: 51.2px; 
  height: 25.6px; 
  background: rgb(241, 44, 44);
  display: block;
  border-radius: 80px;
  position: relative;
}

.switch-label:after {
  content: &quot;&quot;;
  position: absolute;
  top: 4px; 
  left: 4px; 
  width: 17.6px; 
  height: 17.6px; 
  background: #fff;
  border-radius: 70.4px; 
  transition: 0.3s;
}

.switch:checked + .switch-label {
  background: #a5ce1e;
}

.switch:checked + .switch-label:after {
  left: calc(100% - 4px); 
  transform: translateX(-100%);
}

.switch-label:active:after {
  width: 22.4px; 
}

If I use my component like this, React Only executes the callback of the first component even if I click the second or the third component

&lt;div&gt;
&lt;Checkbox key={1} initialState={false} onToggle={()=&gt;{console.log(&quot;One&quot;)}} /&gt;
&lt;Checkbox key={2} initialState={false} onToggle={()=&gt;{console.log(&quot;Two&quot;)}} /&gt;
&lt;Checkbox key={3} initialState={false} onToggle={()=&gt;{console.log(&quot;Three&quot;)}} /&gt;
&lt;/div&gt;

答案1

得分: 0

The id attribute is meant to be unique within an HTML document, so having multiple elements with the same ID can lead to conflicts.

<Checkbox key={1} initialState={false} onToggle={()=>{console.log("One")}} id={'one'}/>
<Checkbox key={2} initialState={false} onToggle={()=>{console.log("Two")}} id={'two'}/>
<Checkbox key={3} initialState={false} onToggle={()=>{console.log("Three")}} id={'three'}/>

Use this instead hardcode 'switch' all your input

<>
<input
type="checkbox"
id={id}
checked={checked}
onChange={handleChange}
/>
<label htmlFor={id}>Toggle</label>
</>

英文:

The id attribute is meant to be unique within an HTML document, so having multiple elements with the same ID can lead to conflicts.

      &lt;Checkbox key={1} initialState={false} onToggle={()=&gt;{console.log(&quot;One&quot;)}} id={&#39;one&#39;}/&gt;
      &lt;Checkbox key={2} initialState={false} onToggle={()=&gt;{console.log(&quot;Two&quot;)}} id={&#39;two&#39;}/&gt;
      &lt;Checkbox key={3} initialState={false} onToggle={()=&gt;{console.log(&quot;Three&quot;)}} id={&#39;three&#39;}/&gt;

Use this instead hardcode 'switch' all your input

&lt;&gt;
  &lt;input
    type=&quot;checkbox&quot;
    id={id}
    checked={checked}
    onChange={handleChange}
  /&gt;
  &lt;label htmlFor={id}&gt;Toggle&lt;/label&gt;
&lt;/&gt;

huangapple
  • 本文由 发表于 2023年6月16日 08:38:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486295.html
匿名

发表评论

匿名网友

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

确定