chobo2 chobo2 - 3 months ago 25
React JSX Question

How to call Name Function(ES 6)?

I have this code

import React from 'react';

import 'materialize-css/sass/materialize.scss';
import 'materialize-css/js/materialize.js';
import 'font-awesome/scss/font-awesome.scss';
import '../styles/main.scss';

export default class AddStorageModal extends React.Component {
constructor() {
super();
this.state = { storageName: "", sharingKey: "" }
}
handleChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
validate() {
if (this.state.storageName === "" && this.state.sharingKey == "") {
console.log("validation error");
return false;
}
this.props.createNewStorage(this.state);
}
resetForm() {
this.state = { storageName: "", sharingKey: "" }
$(function () {
Materialize.updateTextFields();
});
}
render() {
if (this.props.storages.openAddStorageModal) {
$('#add-new-storage-modal').openModal({ dismissible: false });
}
else {
$('#add-new-storage-modal').closeModal();
****resetForm();*****
}
return (
<div id="add-new-storage-modal" className="modal" >
<div className="modal-content">
<h6>Enter your new Storage (Freezer, Pantry, etc.) </h6>
<div className="row">
<form>
<div className="input-field col s12 m12 l12 ">
<input id="storage_name" type="text" value={this.state.storageName} name="storageName" onChange={this.handleChange.bind(this) } />
<label htmlFor="storage_name">Storage Name</label>
</div>
<br />
<h4 className="center">OR</h4>
<h6>Enter in the sharing key you were given.</h6>
<div className="input-field col s12 m12 l12 ">
<input id="sharing_key" type="text" value={this.state.sharingKey} name="sharingKey" onChange={this.handleChange.bind(this) } />
<label htmlFor="sharing_key">Sharking Key</label>
</div>
</form>
</div>
</div>
<div className="modal-footer">
<a href="#!" className="waves-effect waves-green btn-flat left" onClick={() => this.validate() }>Add</a>
<a href="#!" className="waves-effect waves-green btn-flat" onClick={() => this.props.loadAddStorageModal(false) }>Cancel</a>
</div>
</div>
)
}
}


I get this error

AddStorageModal.js:35Uncaught ReferenceError: resetForm is not defined


How do I call resetForm()? I am calling right now in the "else"(just look for the stars)

Answer

You forgot the this.

It should be:

if (this.props.storages.openAddStorageModal) {
    $('#add-new-storage-modal').openModal({ dismissible: false });
}
else {
    $('#add-new-storage-modal').closeModal();
    this.resetForm();
}
Comments