Readdy Write

React This State Variable undefined in Function

02.01.2023 (👁3693)

In React, wenn man vom HTML Code aus eine javascript Function aufruft, bei welcher eine state-variable verwendet werden soll

Fehlermeldung:
Uncaught TypeError: Cannot read properties of undefined (reading 'state')

 

Falsch

    onClickEdit(event) {

        window.location.href = "/edit/" + this.state.idarticle;

    }

Richtig

    //*arrow()-function to get id

    onClickEdit = (event) => {

        //alert('you clicked me');

        //event.preventDefault(); //stops a href=url

        window.location.href = "/edit/" + this.state.idarticle;

        //later:browserHistory.push('/');

    }




Aufruf aus dem Render() Block

<div style={{ float: 'right' }}>

<button type="button" className="btn btn-primary" onClick={this.onClickEdit }>Edit</button>

                                </div>

Ein Bild, das Text enthält.

Automatisch generierte Beschreibung




0,00 €