admin管理员组文章数量:1424429
I am trying to create a custom checkbox, so I can add my own styling and all.
import React, { useState } from "react";
import "./checkbox.module.css";
}
export default props => {
const [isChecked, setIsChecked] = useState(false);
const toggleCheck = () => {
setIsChecked(!isChecked);
};
return (
<>
<span onClick={() => toggleCheck()}>
<input type="checkbox" checked={isChecked} {...props} />
<span />
</span>
</>
);
};
from my main file, I am calling this set of functions.
const handleModifierChange = event => {
console.log("inside change", event);
const selectedID = event.target.value;
};
<Checkbox
name={modifier.name}
value={modifier.id}
onChange={handleModifierChange}
/>
So I want to be able to pass in my onChange prop though to the actual input field but be notified inside the actual handleModifierChange when the checkbox had a change.
I was trying to follow this stackoverflow post, Styling a checkbox in a ReactJS environment
To the answer, someone asked a similar question I am trying to figure out. "Amazing however, I am unable to get the e.target.name or e.target.id while listening to an onChange event. It returns undefined as the input is not in the DOM. So what do we do in that case?".
I am trying to create a custom checkbox, so I can add my own styling and all.
import React, { useState } from "react";
import "./checkbox.module.css";
}
export default props => {
const [isChecked, setIsChecked] = useState(false);
const toggleCheck = () => {
setIsChecked(!isChecked);
};
return (
<>
<span onClick={() => toggleCheck()}>
<input type="checkbox" checked={isChecked} {...props} />
<span />
</span>
</>
);
};
from my main file, I am calling this set of functions.
const handleModifierChange = event => {
console.log("inside change", event);
const selectedID = event.target.value;
};
<Checkbox
name={modifier.name}
value={modifier.id}
onChange={handleModifierChange}
/>
So I want to be able to pass in my onChange prop though to the actual input field but be notified inside the actual handleModifierChange when the checkbox had a change.
I was trying to follow this stackoverflow post, Styling a checkbox in a ReactJS environment
To the answer, someone asked a similar question I am trying to figure out. "Amazing however, I am unable to get the e.target.name or e.target.id while listening to an onChange event. It returns undefined as the input is not in the DOM. So what do we do in that case?".
Share Improve this question asked Aug 10, 2019 at 20:49 Just_IceJust_Ice 5731 gold badge6 silver badges15 bronze badges1 Answer
Reset to default 5So, you probably have a problem because of re-render of you ponent after state change. You can try to get rid of handling span's onClick
, but just call onClick
callback in checkbox change handler:
export default ({ onChange, ...checkboxProps }) => {
const [isChecked, setIsChecked] = useState(false);
const changeHandler = () => {
setIsChecked(!isChecked);
onChange && onChange(event);
};
return (
<>
<span>
<input
type="checkbox"
checked={isChecked}
onChange={changeHandler}
{...checkboxProps}
/>
<span />
</span>
</>
);
};
You can even pass checkbox value instead of event instance:
onChange && onChange(isChecked && props.value);
本文标签: javascriptHow to create custom checkbox in react hooksStack Overflow
版权声明:本文标题:javascript - How to create custom checkbox in react hooks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745396778a2656851.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论