最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React checkbutton onChange event returns synthetic event - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Aug 6, 2019 at 20:38 Capan asked Aug 6, 2019 at 20:24 CapanCapan 7361 gold badge17 silver badges36 bronze badges 1
  • You may want to look here – Gabriel Arghire Commented Dec 21, 2020 at 15:09
Add a comment  | 

3 Answers 3

Reset to default 8

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

  • event.currentTarget docs
  • a nice post about event.target vs event.currentTarget

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>

Try to do it:

<input onChange={e => onChangeHandler(e.target.value)}></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
      };
    });   }
发布评论

评论列表(0)

  1. 暂无评论