简体   繁体   English

设置React / Webpack / Babel并创建自定义组件

[英]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.

相关问题 努力使用Webpack设置reactjs和babel - Struggling setting up reactjs, babel using webpack 如何使用React和Webpack设置Babel 6 stage 0 - How to set up Babel 6 stage 0 with React and Webpack 无法设置React / Babel / Webpack - Can't set up React/Babel/Webpack 设置babel-plugin-styled-components + Typescript + create-react-app - Setting up babel-plugin-styled-components + Typescript + create-react-app React SSR express,webpack,babel babel 不喜欢我的组件中的 CSS - React SSR express,webpack,babel babel doesn't like the CSS in my components 使用 webpack 或 babel 创建 react 组件 npm 包不起作用 - Creating a react component npm package with webpack or babel doesn't work 故事书 - 使用自定义 webpack/babel 的打字稿项目中没有出现任何故事 - Storybook - no stories showing up in typescript project with custom webpack / babel 使用 React 和 Webpack 设置 Airbnb ESLint - Setting up Airbnb ESLint with React and Webpack 设置React和Babel时出现意外的令牌错误 - Unexpected token error when setting Up React and Babel 在ReactJS应用程序中设置jsx文件。 堆栈:babel,webpack,react,react-hot,jsx,Sass - Setting jsx file in ReactJS application. Stack: babel, webpack, react, react-hot, jsx, Sass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM