reactjsreact-router-dom

typeError: cannot read property 'users' of null in React


I'm trying to create a change password page in react and i'm getting typeError: cannot read property 'users' of null. The code works for other form pages(where i'm doing PUT and CREATE) but not this one

I tried binding the submit handler to the this keyword but that didn't work. Also tried binding the handlePasswordChange to the this keyword

./formchange

import React from "react";
import { Link } from "react-router-dom";
var createReactClass = require("create-react-class");
var FormChange = createReactClass({
  //setting initial state
  getInitialState() {
    return {
      password: {}
    };
  },
  handlePasswordChange(e) {
    this.setState({
      password: e.target.value
    });
  },
  handleSubmit(e) {
    e.preventDefault();
    this.props.onSubmit(this.state);
    this.props.history.push("/");
  },
  render() {
    return (
      <form
        name="categories_post"
        className="form-horizontal"
        onSubmit={this.handleSubmit}
      >
        <div id="change_password">
          <div className="form-group">
            <label
              className="col-sm-2 control-label required"
              htmlFor="password"
            >
              Password
            </label>
            <div className="col-sm-10">
              <input
                type="text"
                value={this.state.password}
                onChange={this.handlePasswordChange}
                id="password"
                className="form-control"
              />
            </div>
          </div>
          <button
            type="submit"
            id="formChangeSubmit"
            className="btn btn-default"
          >
            Submit
          </button>
        </div>
      </form>
    );
  }
});
export default FormChange;

./passwordupdate

import React from "react";
import { updateUsers, fetchUsers } from "./actions/appactions";
import FormChange from "./formchange";
var createReactClass = require("create-react-class");
const Update = createReactClass({
  getIntitialState() {
    return {
      users: {}
    };
  },
  componentWillReceiveProps(props) {
    this.setState(props);
  },
  componentDidMount() {
    fetchUsers(this.props.match.params.usersId)
      .then(data => {
        this.setState(state => {
          state.users = data;
          return state;
        });
      })
      .catch(err => {
        console.error("error", err);
      });
  },
  handleSubmit(data) {
    updateUsers(this.state.users.id, data);
  },
  render() {
    return (
      <div>
        <FormChange
          onSubmit={this.handleSubmit.bind}
          password={this.state.users.password}
        />
      </div>
    );
  }
});
export default Update;

//fetchusers function
export function fetchUsers(id) {
  return fetch("https://localhost:44341/api/users/" + id, {
    method: "GET",
    mode: "cors"
  })
    .then(res => res.json())
    .catch(err => err);
}

Solution

  • The problem was in ComponentDidMount(). The state was always null, had to change it to this

     componentDidMount() {
        fetchTheUsers(this.props.match.params.usersId)
          .then(data => {
            this.setState({
              users: data
            });
          })
    

    I did it that way initially because that's how it worked for my other update files. Hope this is useful to someone else.