score:280
To get the checked state of your checkbox the path would be:
this.refs.complete.state.checked
The alternative is to get it from the event passed into the handleChange
method:
event.target.checked
score:1
In material ui, state of checkbox can be fetched as
this.refs.complete.state.switched
score:2
onChange will not call handleChange on mobile when using defaultChecked. As an alternative you can can use onClick and onTouchEnd.
<input onClick={this.handleChange} onTouchEnd={this.handleChange} type="checkbox" defaultChecked={!!this.state.complete} />;
score:9
In case someone is looking for a universal event handler the following code can be used more or less (assuming that name property is set for every input):
this.handleInputChange = (e) => {
item[e.target.name] = e.target.type === "checkbox" ? e.target.checked : e.target.value;
}
score:14
In the scenario you would NOT like to use the onChange handler on the input DOM, you can use the onClick
property as an alternative. The defaultChecked
, the condition may leave a fixed state for v16 IINM.
class CrossOutCheckbox extends Component {
constructor(init){
super(init);
this.handleChange = this.handleChange.bind(this);
}
handleChange({target}){
if (target.checked){
target.removeAttribute('checked');
target.parentNode.style.textDecoration = "";
} else {
target.setAttribute('checked', true);
target.parentNode.style.textDecoration = "line-through";
}
}
render(){
return (
<span>
<label style={{textDecoration: this.props.complete?"line-through":""}}>
<input type="checkbox"
onClick={this.handleChange}
defaultChecked={this.props.complete}
/>
</label>
{this.props.text}
</span>
)
}
}
I hope this helps someone in the future.
score:22
If you have a handleChange
function that looks like this:
handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value,
});
}
You can create a custom onChange
function so that it acts like an text input would:
<input
type="checkbox"
name="check"
checked={this.state.check}
onChange={(e) => {
this.handleChange({
target: {
name: e.target.name,
value: e.target.checked,
},
});
}}
/>
score:48
It's better not to use refs in such cases. Use:
<input
type="checkbox"
checked={this.state.active}
onClick={this.handleClick}
/>
There are some options:
checked
vs defaultChecked
The former would respond to both state changes and clicks. The latter would ignore state changes.
onClick
vs onChange
The former would always trigger on clicks.
The latter would not trigger on clicks if checked
attribute is present on input
element.
Source: stackoverflow.com
Related Query
- React Checkbox not sending onChange
- React Material-UI checkbox onChange event does not fire
- Why is my React checkbox onChange handler firing on render and then not when the box is clicked?
- checkbox onChange in <legend> inside disabled <fieldset> not firing in Firefox with React
- Checkbox not responding to onChange - React
- React component not sending variable value with onChange event
- React Checkbox Does Not Update
- React hooks useState not updating with onChange
- React input type file onChange not firing
- React select onChange is not working
- Checkbox onChange event is not handled by handleChange props by Formik
- onKeyUp not working in React where onChange did
- React div container onClick conflicts with checkbox onChange
- React onChange functions with ES6 arrows or not
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- React Formik checkbox group does not turn into an array of invidual checked or unchecked elements
- React 16 Radio Button onChange not working
- Sending state with react router not working
- React radio button onChange event does not bind on first change of radio button
- Checkbox state not toggle. Material UI React
- React Native + Expo + Axios file upload not working because axios is not sending the form data to the server
- React - < Input> value is not getting updated on onChange event
- React toggle select field not removing when a specific checkbox is unchecked
- React with Fluent UI Form onChange not working (very simple code)
- React Submit Form Not Sending POST Request
- onChange callback not firing in React component
- React - sending down ref as a prop not working
- Bootstrap's toggle button group not calling onChange event in react
- React input onchange simulation not updating value using Jest and enzyme
- onChange is not triggering in react js
More Query from same tag
- Javascript: Filter list of objects by a list of filter objects
- How do you implement a Higher-Order-Component in React?
- Why is the useEffect changes to state in the Parent not happening in the Child?
- Input type number not updating value on enter
- How can i can change the value of a variable with if/else to return certain div in react
- How do I render reach native components properly
- How to loop and render different components from array in ReactJs?
- Is there a way to shorthand useSelector instead of causing unnecessary renders?
- fetch() in js (react hooks) - can I access data in third then?
- How to check if a child element exists in parent in React?
- React: _this.state is undefined
- Multiple params with React Router
- d3js Collapsible tree dynamic data from API not working
- fetching with response from a previous fetch
- Can't render an array inside of the ReactJS state
- How to capture the backspace event in onChange tag of Form for Reactjs applications
- Typescript Error in React useScroll hook: Cannot invoke an expression whose type lacks a call signature
- Usage of GitHub Primer React Components with Octions
- react useState not re rendering
- How to pass state and props from app.js to other files?
- scrollIntoView using React refs
- How to send props to a component while using Route to load?
- module not found - webpack ts-loader @material-ui
- Should I be using JSON or XML when the order of the properties, of the server-returned object, matters?
- Best technique to avoid function creation in render() method when arguments are involved
- How can I show my backend api errors via the UI with axios React?
- How to "scale" all the elements in Material-UI?
- Where and how should I add JSON-LD schema in my Create React App?
- bootstrap form button is outside of the form div
- How to make an Scale from the center while hovering using framer motion