[英]Setting up React / Webpack / Babel and creating custom components
I'm setting up a really simple React / Webpack / Babel project following this great guide here - https://www.robinwieruch.de/minimal-react-webpack-babel-setup/ 我在这里设置了这个伟大的指导下一个非常简单的反应,和/的WebPack /巴贝尔项目- https://www.robinwieruch.de/minimal-react-webpack-babel-setup/
I'm off the ground with this in my index.js 我在index.js中对此一无所知
import React from 'react';
import ReactDOM from 'react-dom';
const title = 'My Minimal React Webpack Babel Setup';
ReactDOM.render(
<div>
{title}
<p>Another test</p>
</div>,
document.getElementById('app')
);
I've got this rendering on my localhost - fab! 我已经在我的localhost-fab上获得了此渲染!
So what I wanted to do next is set up my own component, I created a folder in the same root as the index.js called 'components' and created a file called 'testComponent.js' which looks like this 因此,我接下来要做的就是设置自己的组件,在与index.js相同的根目录下创建一个名为“ components”的文件夹,并创建一个名为“ testComponent.js”的文件,如下所示
import React from 'react';
export default class testComponent extends Component {
render() {
return (
<div>
Test component
</div>
)
}
}
I then added this to my index.js by importing the component and adding it to the render function but this seems to break my app 然后我通过导入组件并将其添加到render函数将其添加到index.js中,但这似乎破坏了我的应用程序
import React from 'react';
import ReactDOM from 'react-dom';
import TestComponent from './components/testComponent';
const title = 'My Minimal React Webpack Babel Setup';
ReactDOM.render(
<div>
{title}
<p>Another test</p>
<TestComponent />
</div>,
document.getElementById('app')
);
I'd really like to play around with .jsx and I think this is the set up guide for me but I can't figure out why this breaks and I feel like I'm missing a step somewhere .. 我真的很想使用.jsx,我认为这是我的设置指南,但我不知道为什么会中断,我觉得我在某个地方缺少一步。
Your TestComponent script doesn't import Component from 'react'. 您的TestComponent脚本不会从“反应”中导入组件。 You need to either change the import statement to
您需要将import语句更改为
import React, { Component } from 'react';
Or, you need to change "extends Component" to "extends React.Component" 或者,您需要将“扩展组件”更改为“扩展React.Component”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.