Javascript 在REACT中渲染有困难
我无法渲染名为Butcher Shop的组件。为了使它不为null或未定义,我无法找出我缺少了什么。有人能发现错误吗?没有任何内容正在打印到DOM。这是我得到的错误:TypeError:Super表达式必须为null或函数,而不是未定义的 40 |出口级屠宰厂扩展反应组件{Javascript 在REACT中渲染有困难,javascript,reactjs,dom,render,Javascript,Reactjs,Dom,Render,我无法渲染名为Butcher Shop的组件。为了使它不为null或未定义,我无法找出我缺少了什么。有人能发现错误吗?没有任何内容正在打印到DOM。这是我得到的错误:TypeError:Super表达式必须为null或函数,而不是未定义的 40 |出口级屠宰厂扩展反应组件{ 你有一个简单的书写错误,应该是 导出类ButcherShop扩展React.Component{ 请注意大写的C我看到了几个问题,您还需要将类转换为类名。如果我在InforontofYou的return中添加了一些文本,它会
你有一个简单的书写错误,应该是 导出类ButcherShop扩展React.Component{
请注意大写的C我看到了几个问题,您还需要将类转换为类名。如果我在InforontofYou的return中添加了一些文本,它会呈现出来。请查看您的“return”以查看它是否正确主要问题:组件应该大写: 另外,考虑下面的语法
export class ButcherShop extends React.Component{
render() {
return
<div class="butcher-shop">
<div>
<p>'Hello! We have the following products for sale today'</p>
<ul>
<li>'Tenderloin'</li>
<li>'Short ribs'</li>
<li>'Beef shin'</li>
<li>'Ribeye'</li>
</ul>
</div>
</div>
}
}
这是因为您没有返回在渲染方法中创建的元素 将所有React.createElement元素保存到一个变量中,并在返回时使用该变量:
export class ButcherShop extends React.Component{
render() {
const content = React.createElement('div', {},
React.createElement('p', {}, 'Hello! We have the following products for sale today'),
React.createElement('ul', {},
[
React.createElement('li', {}, 'Tenderloin'),
React.createElement('li', {}, 'Short ribs'),
React.createElement('li', {}, 'Beef shin'),
React.createElement('li', {}, 'Ribeye')
]));
return <div class="butcher-shop">{content}</div>
}
}
组件呈现方法的工作方式与此相同。您需要实际返回您创建的元素。我建议您最好使用JSX。 我为您留下一个链接,供您学习如何使用: . 以下是使用JSX时代码的外观示例:
import React from 'react';
import ReactDOM from 'react-dom';
const BUTCHER_PRODUCTS = [
'Tenderloin',
'Short ribs',
'Beef shin',
'Ribeye'
];
export class OlderCoaster extends React.Component {
render(){
return(
<div className="oldercoaster">
<p>Two grannies having the time of their life!</p>
<p>Passengers:</p>
<ul>
<li>Anges</li>
<li>miruel</li>
</ul>
</div>
)}};
export class InFrontOfYou extends React.Component{
render() {
return(
<div>
<p>You shouldnt look too far</p>
<p>Sometimes, the soultion is right in from of you</p>
</div>
)
);
};
export class ButcherShop extends React.Component{
render() {
return(
<div className="butcher-shop">
<p>Hello! We have the following products for sale today</p>
<ul>
<li>Tenderloin</li>
<li>Short ribs</li>
<li>Beef shin</li>
<li>Ribeye</li>
</ul>
</div>
)
}
ReactDOM.render(
<div>
<OlderCoaster/>
<InFrontOfYou/>
<ButcherShop/>
</div>,
document.getElementById('global')
);
这不应该是第40行的React.Component而不是React.Component吗?太棒了。谢谢!为什么不使用JSX来代替调用JS代码。它易于阅读和维护。这样就消除了错误,但是页面仍然是空白的。我感觉这与顶部的butcher_products数组有关。@davidhazlettjr这是因为你不是返回在渲染方法中创建的元素。将所有React.createElement元素保存到一个变量中,并在返回中使用该变量:return{elements}
ReactDOM.render(
React.createElement('div', {}, [
React.createElement(OlderCoaster),
React.createElement(InFrontOfYou),
React.createElement(ButcherShop)
]),
document.getElementById('global')
);
import React from 'react';
import ReactDOM from 'react-dom';
const BUTCHER_PRODUCTS = [
'Tenderloin',
'Short ribs',
'Beef shin',
'Ribeye'
];
export class OlderCoaster extends React.Component {
render(){
return(
<div className="oldercoaster">
<p>Two grannies having the time of their life!</p>
<p>Passengers:</p>
<ul>
<li>Anges</li>
<li>miruel</li>
</ul>
</div>
)}};
export class InFrontOfYou extends React.Component{
render() {
return(
<div>
<p>You shouldnt look too far</p>
<p>Sometimes, the soultion is right in from of you</p>
</div>
)
);
};
export class ButcherShop extends React.Component{
render() {
return(
<div className="butcher-shop">
<p>Hello! We have the following products for sale today</p>
<ul>
<li>Tenderloin</li>
<li>Short ribs</li>
<li>Beef shin</li>
<li>Ribeye</li>
</ul>
</div>
)
}
ReactDOM.render(
<div>
<OlderCoaster/>
<InFrontOfYou/>
<ButcherShop/>
</div>,
document.getElementById('global')
);