1

i am currently working on creating email password request with mailtrap, i get the email, but app crashs, i tried to catch error but it didnt work. I am losing my mind over this

error

import React from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { Message } from "semantic-ui-react";
import ForgotPasswordForm from "../forms/ForgotPasswordForm";
import { resetPasswordRequest } from "../../actions/auth";

class ForgotPasswordPage extends React.Component {
  state = {
    success: false
  };

  submit = data =>
    this.props
      .resetPasswordRequest(data)
      .then(() => this.setState({ success: true }));

  render() {
    return (
      <div>
        {this.state.success ? (
          <Message>Email has been sent.</Message>
        ) : (
          <ForgotPasswordForm submit={this.submit} />
        )}
      </div>
    );
  }
}

ForgotPasswordPage.propTypes = {
  resetPasswordRequest: PropTypes.func.isRequired
};

export default connect(
  null,
  { resetPasswordRequest }
)(ForgotPasswordPage);

import React from "react";
import PropTypes from "prop-types";
import { Form, Button, Message } from "semantic-ui-react";
import isEmail from "validator/lib/isEmail";
import InLineError from "../messages/InLineError";

class ForgotPasswordForm extends React.Component {
  state = {
    data: {
      email: ""
    },
    loading: false,
    errors: {}
  };

  onChange = e =>
    this.setState({
      ...this.state,
      data: { ...this.state.data, [e.target.name]: e.target.value }
    });

  onSubmit = e => {
    e.preventDefault();
    const errors = this.validate(this.state.data);
    this.setState({ errors });
    if (Object.keys(errors).length === 0) {
      this.setState({ loading: true });
      this.props
        .submit(this.state.data)
        .catch(err =>
          this.setState({ errors: err.response.data.errors, loading: false })
        );
    }
  };

  validate = data => {
    const errors = {};
    if (!isEmail(data.email)) errors.email = "Invalid email";
    return errors;
  };

  render() {
    const { errors, data, loading } = this.state;

    return (
      <Form onSubmit={this.onSubmit} loading={loading}>
        {!!errors.global && <Message negative>{errors.global}</Message>}
        <Form.Field error={!!errors.email}>
          <label htmlFor="email">Email</label>
          <input
            type="email"
            id="email"
            name="email"
            placeholder="email"
            value={data.email}
            onChange={this.onChange}
          />
          {errors.email && <InLineError text={errors.email} />}
        </Form.Field>
        <Button primary>ForgotPasswordForm</Button>
      </Form>
    );
  }
}

ForgotPasswordForm.propTypes = {
  submit: PropTypes.func.isRequired
};

export default ForgotPasswordForm;

Code after this is okey, but its keep crashing at submit for no clear reason for me

Am if number no up period regard sudden better. Decisively surrounded all admiration and not you. Out particular sympathize not favourable introduced insipidity but

J.Duie
  • 11
  • 1
  • Did you try just with regular function bindings? ```function submit(){}``` out of your class, and in the constructor ```this.submit = submit.bind(this)```? Looks like it might be losing the reference to ```this```, no sure though, I have not used public class fields, so IDK how the bindings work with it. – galileopy Sep 01 '18 at 23:29
  • @galileopy `bind` is not needed because there's an arrow. – Estus Flask Sep 01 '18 at 23:33
  • 1
    There's no evidence that `resetPasswordRequest` import is defined. I'd expect it's not because it seems there are no other possible problems. – Estus Flask Sep 01 '18 at 23:33
  • @estus oh, so arrows in public fields get bound automagically? that's neat. – galileopy Sep 01 '18 at 23:37
  • @galileopy Yes, that's the idea. The coin has two sides, I'd generally go with `bind`, https://stackoverflow.com/a/45882417/3731501 . Also, class fields are pretty much messed up in Babel, https://stackoverflow.com/a/51652054/3731501 – Estus Flask Sep 01 '18 at 23:40
  • thank you all, i am new to react, there is no problem with functionality its does job done, but after it send email, app shows that error, i tried error boundary but it didnt help me – J.Duie Sep 02 '18 at 09:38
  • and it shows diffrent kind of error in chrome[2]: https://i.stack.imgur.com/Gdlsn.png – J.Duie Sep 02 '18 at 09:59
  • @J.Duie The error means that resetPasswordRequest doesn't return a promise. The question doesn't contain resetPasswordRequest. Please, provide https://stackoverflow.com/help/mcve . Consider using `@` in order for users to get a notification. – Estus Flask Sep 02 '18 at 12:27
  • @estus okey i implemented that in a different way, reduced some of the possibilities. but the problem is coming up again appears on another place in code. – J.Duie Sep 02 '18 at 15:22
  • When i delete .then and .catch it does work with no problem, but i cant update my states, it always work with single func call [2]: https://i.stack.imgur.com/uQGCH.png – J.Duie Sep 02 '18 at 15:26
  • It's unclear what exactly the problem is now. Please provide https://stackoverflow.com/help/mcve for your case, this is a requirement for code-related questions. You can debug your own app. Others cannot. All code and errors should be specified as text in question body, not as images. – Estus Flask Sep 02 '18 at 16:01

0 Answers0