DOM元素无法访问状态对象-ReactJS

时间:2019-10-06 09:34:18

标签: reactjs react-hooks

我有一个有状态的反应单页程序,由于某种原因,我可以在DOM中JSON.stringify我的applicant对象,但是如果我尝试通过引用从applicant访问任何内容,我什么也没得到。

具体来说,我是指我尝试使用applicant.bio时一无所获。

以下是组件:

// src/core/Project.js single project component

import React, {useState, useEffect} from 'react';
import Layout from '../core/Layout';
import {getProjects, read, listRelated, getBraintreeClientToken} from '../core/apiCore';
import {getApplication} from '../user/apiUser';
import Card from '../core/Card';
import {isAuthenticated} from '../auth';
import {Link} from 'react-router-dom';
import moment from 'moment';

const SelectApplicant = (props) => {
  const [applicant, setApplicant] = useState({});
  const [error, setError] = useState(false);
  const {user: {_id, name, email, role_type}} = isAuthenticated();
  const token = isAuthenticated().token;

  const {skills, bio, applicantName, studying} = applicant;

  const loadApplicant = (applicationId, userId) => {
    getApplication(userId, applicationId, token).then(data => {
      if(data.error) {
        setError(data.error);
      } else {
        setApplicant(data);
      }
    });
  };

  useEffect(() => {
    const applicationId = props.match.params.applicationId;
    loadApplicant(applicationId, _id);
  }, [props]);

  return (
      <Layout title="Select Applicant." description="One step closer." className="container">
          <div className="container">
         <div className="row">
                        <>
                        <div className="card col-4 mb-3">
                          <div className="card-header name">{applicant.applicantName && applicant.applicantName}</div>
                          <div className="card-body">
                              <p style={{fontSize:"smaller"}} 
                                 className="Lead"> 
                                 Studying {applicant.studying} at {applicant.university}
                              </p>
                          <hr/>
                        <p style={{fontSize:"small",fontStyle: "italic"}} 
                             className="black-10">
                             "Tell us why you'd make a great fit..."
                             </p>
                          <p className="Lead" style={{fontSize: "small"}}>{applicant.bio}</p>
                              <hr/>

                                                        {JSON.stringify(applicant)}

                              {applicant.skills && applicant.skills.map((s, j) => {
                                return(
                                <>  
                                  <p style={{fontSize: "smaller", fontStyle:"italic"}} 
                                     className="black-10" key={j}>
                                     "{s}..."
                                   </p>
                                  <p style={{fontSize: "smaller"}} 
                                     className="Lead">
                                     {s.experience[j]}
                                     </p>
                                  <hr/>
                                </>
                                )
                              })}
                          <p style={{fontSize:"smaller"}} className="sm">Posted {moment(applicant.createdAt).fromNow()}</p>
                          <button className="btn btn-sm btn-outline-dark">View Profile</button> 
                          <Link to={`admin/application/${applicant._id}`}>
                          <button className="btn btn-sm btn-outline-dark ml-2">Select Applicant</button> 
                          </Link>
                          </div>
                        <hr/>
                        </div>
                        </>
                    </div>
                </div>
      </Layout>
  );
};

export default SelectApplicant;

0 个答案:

没有答案