1

In the same component, I get the value of this.props.users from redux store in render() function but can't get the value in constructor() function. I mean in render function I got value from this.props.users but in constructor function I got null. Here is my code...

import React, { Component } from "react";
import { connect } from "react-redux";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faFaceSmile, faFaceFrown } from "@fortawesome/free-solid-svg-icons";
import axios from "axios";

const mapStateToProps = (state) => {
    return {
        userId: state.userId,
        users: state.users,
    };
};

class EditProfile extends Component {
    constructor(props) {
        super(props);
        this.state = {
            name:
                this.props.users == null
                    ? ""
                    : this.props.users.find(
                          (item) => item.userId === this.props.userId
                      ).name,
            email:
                this.props.users == null
                    ? ""
                    : this.props.users.find(
                          (item) => item.userId === this.props.userId
                      ).email,
            gander:
                this.props.users == null
                    ? ""
                    : this.props.users.find(
                          (item) => item.userId === this.props.userId
                      ).gander,
            message: null,
        };
    }

    render() {
        return (
            <div className="col">
                <h4 className="h4 my-2">
                    {this.props.users == null
                        ? ""
                        : this.props.users.find(
                              (item) => item.userId === this.props.userId
                          ).name}
                    's Info
                </h4>
                
            </div>
        );
    }
}

export default connect(mapStateToProps)(EditProfile);
Hasan Aga
  • 718
  • 8
  • 27

0 Answers0