【问题标题】:Unable to load React class component inside other class component无法在其他类组件中加载 React 类组件
【发布时间】:2017-09-29 18:18:08
【问题描述】:

我是新手,我正在尝试执行下面的代码,但是我无法将一个类组件加载到另一个类组件中,下面是我的 js 文件,请告诉我我的代码有什么问题

任何人都可以帮助反应基本教程和反应示例

Apicall.js

import React from "react";
import { fetchjson } from '../helpers/helpers';
import posts from "../components/posts";
export default class Apicall extends React.Component {
  constructor() {
    super();
    this.state = { items: [] };
}
 componentDidMount() {

fetchjson('posts').then((data) => {
  this.setState({items: data});
});

}

 render() {
return (
  <div>
  <h1>API Call</h1>
  {this.state.items.map(item => 
  <div key={item.id}>
  <h4>{item.title}</h4>
  <p>{item.body}</p>
  <posts posttitle={item}/>
  </div>
  )} 
  </div>
);
}
}

posts.js

import React from "react";
export default class posts extends React.Component {
constructor(props) {
super(props);
 }
render() {
 return (
   <div>{this.props.posttitle.title}</div>
 );
}
}

以下是我得到的输出,无法从帖子组件加载渲染

【问题讨论】:

  • 您遇到了什么错误或行为?
  • 您是否尝试在返回之前执行您的.map()
  • @panther 以上是我现在得到的行为,我无法从帖子组件加载内容

标签: reactjs routes components react-router


【解决方案1】:

用户定义的 React 组件must begin with an Uppercase character

改变

import posts from "../components/posts";

import Posts from "../components/posts";

<posts posttitle={item}/>

<Posts posttitle={item}/>

您可能还想更改类名本身(class posts ...class Posts ...,但在使用 export default 时不是必需的(导入器声明使用的实际标识符。

【讨论】:

  • 非常感谢迈克尔。
猜你喜欢
  • 1970-01-01
  • 2018-08-31
  • 2020-06-26
  • 2019-03-04
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多