将功能组件转换为类组件(ReactJS)时出现问题

时间:2020-06-27 04:56:06

标签: javascript reactjs components state

这是我的功能组件。效果很好


    import React from "react";

    function RecommendationCard(props) {
      const { name, designation, company, message } = props.recommendation;
      return (
        <div className="col-12 col-md-4">
          <div className="card shadow h-100">
            <div className="card-body">
              <h4 className="card-text">{message}</h4>
              <p className="card-text text-secondary mb-0">{name}</p>
              <p className="card-text text-secondary">
                {designation} at {company}
              </p>
            </div>
          </div>
        </div>
      );
    }

    export default RecommendationCard;

下面的代码是转换后的状态组件。在使用props方面,我像状态一样使用了context.js文件中的数据。它包含所有数据,如名称,公司,名称,推荐。所以我正在通过消费者使用它。但这不显示属性。


  import React, { Component } from 'react';
  import { Consumer } from '../context';

  class RecommendationCardextends Component {
    state = {
      name:"",
      message:"",
      designation:"",
      company:"",
    }

      render() {
        return(
          <Consumer>
          {(value) =>{
              const { name, designation, company,message } = this.state;
              
              return (
                <div className="col-12 col-md-4">
                <div className="card shadow h-100">
                  <div className="card-body">
                    <h4 className="card-text">{message}</h4>
                    <p className="card-text text-secondary mb-0">{name}</p>
                    <p className="card-text text-secondary">
                      {designation} at {company}
                    </p>
                  </div>
                </div>
              </div>
              );
          }}
        </Consumer>
        );
    }
  }
    
  
  export default RecommendationCard;

我请您解决此问题并为此提供帮助。预先谢谢你。

2 个答案:

答案 0 :(得分:0)

我在codeandbox上做了一个工作示例: https://codesandbox.io/s/exciting-chatelet-7il42?file=/src/App.js

应用程序:

import React from "react";
import "./styles.css";
import RecommendationCard from "./components/RecommendationCard";
import {MyContext} from "./context"

const initialState = {
  message: "default message",
  company: "default company",
  designation: "default designation",
  name: "default name"
};

export { MyContext };

export default function App() {
  return (
    <div className="App">
      <MyContext.Provider value={initialState}>
        <RecommendationCard />
      </MyContext.Provider>
    </div>
  );
}

推荐卡:

import React, { Component } from "react";
import { MyContext } from "../context";

class RecommendationCard extends Component {
  
  static contextType = MyContext;

  render() {
    return (
      <div className="col-12 col-md-4">
        <div className="card shadow h-100">
          <div className="card-body">
            <h4 className="card-text">{this.context.message}</h4>
            <p className="card-text text-secondary mb-0">{this.context.name}</p>
            <p className="card-text text-secondary">
              {this.context.designation} at {this.context.company}
            </p>
          </div>
        </div>
      </div>
    );
  }
}

export default RecommendationCard;

上下文:

import React from "react";

export const MyContext = React.createContext();

答案 1 :(得分:0)

      <Consumer>
        {({ name, designation, company, message }) => (
          <div className="col-12 col-md-4">
            <div className="card shadow h-100">
              <div className="card-body">
                <h4 className="card-text">{message}</h4>
                <p className="card-text text-secondary mb-0">{name}</p>
                <p className="card-text text-secondary">
                  {designation} at {company}
                </p>
              </div>
            </div>
          </div>
        )}
      </Consumer>

假设您不打算对其进行任何操作,那么在您提供的示例中,您所在的州将过时。您所有的数据都在value内部。我个人觉得使用Hook API useContext会变得更加容易。