简体   繁体   中英

React checkbutton onChange event returns synthetic event

I have a React checkbox component. I'm trying to change the state on every click and run a function based on the previous state. On the React everytime I click on the button I'm trying to return event to my console.log function in the index.js.

But it's always returning synthetic event and I can't reach the event.target.value.

My index.js and props are;

ReactDOM.render(<ReactCheckBox
    isChecked={false}
    textCheck="Checked"
    textUncheck="Checked"
    onCheckedRun={event => console.log(event.target)}
    onUncheckedRun={event => console.log(event.target)}
    buttonPosition="right"
/>, document.getElementById('myCheckBox'))

and my ReactCheckBox component is;

import React, { Component } from 'react'

class ReactCheckBox extends Component {
    constructor(props) {
        super(props)
        this.state = {
            checked: ""
        }
        this.onChangeHandler = this.onChangeHandler.bind(this)

    }
    componentDidMount() {
        this.setState({
            checked: this.props.isChecked
        })
    }

    onChangeHandler(event) {
        this.setState(function (previousState, props) {
            console.log('State: ',this.state.checked)
            if (previousState.checked === true) {
                props.onCheckedRun(event)
            } else {
                props.onUncheckedRun(event)
            }
            return {
                checked: !this.state.checked
            }
        }
        )
    }

    render() {
        if (this.props.disabled === "true") {
            return (
                <div>
                    <div className="form-check">
                        <label className="form-check-label">
                            <div className="uniform-checker border-primary-600 text-primary-800">
                                <span className="checked">
                                    <input onChange={this.onChangeHandler} type="checkbox" className="form-check-input-styled-primary" defaultChecked={this.state.checked ? 'checked' : 'unchecked'} data-fouc="" disabled></input>
                                </span>
                            </div>
                            Primary checkbox
                        </label>
                    </div>
                </div>

            )
        }
        else {
            return (
                <div>
                    <div>
                        <div className="form-check">
                            <label className="form-check-label">
                                <div className="uniform-checker border-primary-600 text-primary-800">
                                    <span className={this.state.checked ? 'checked' : 'unchecked'}>
                                        <input onChange={this.onChangeHandler} type="checkbox" className="form-check-input-styled-primary" defaultChecked={this.state.checked ? 'checked' : 'unchecked'} ></input>
                                    </span>
                                </div>
                                Primary checkbox
                            </label>
                        </div>
                    </div>
                </div>
            )
        }
    }
}

export default ReactCheckBox

Has anyone had the same issue? How can I over come synthetic event and get the real event so I can reach value of it?

UPDATE

Synthetic error event

index.js:1375 Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the method currentTarget on a released/nullified synthetic event. This is a no-op function. If you must keep the original synthetic event around, use event.persist(). See fb.me/react-event-pooling for more information.

Try to do it:

<input onChange={e => onChangeHandler(e.target.value)}></input>

Instead of event.target try event.currentTarget to get the specific targeted element.

Update: In relation to your update and the error, can you try doing this: instead of using <input onChange={this.onChangeHandler}> do this:

<input onChange={(e)=>{this.onChangeHandler(e)}}>

or just:

<input onChange={(e)=>{onchangeHandler(e)}}>

So the full line would be:

<input onChange={()=>{this.onChangeHandler(e)}} type="checkbox" className="form-check-input-styled-primary" defaultChecked={this.state.checked ? 'checked' : 'unchecked'} ></input>

If you want the result, Yoy May try This .

console.log(event.currentTarget.checked);

it will return true or false

no Neeed to do

<input onChange={(e)=>{onchangeHandler(e)}}>

UPDATE

Synthetic error event

index.js:1375 Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the method currentTarget on a released/nullified synthetic event. This is a no-op function. If you must keep the original synthetic event around, use event.persist(). See fb.me/react-event-pooling for more information.

simple just call.

 onChangeHandler(event) {
    event.persist();
    this.setState(function(previousState, props) {
      console.log("State: ", this.state.checked);
      if (previousState.checked === true) {
        props.onCheckedRun(event.currentTarget.checked);
      } else {
        props.onUncheckedRun(event.currentTarget.checked);
      }
      return {
        checked: !this.state.checked
      };
    });   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM