Code Monkey home page Code Monkey logo

curso-reactjs-nextjs-project-3-2022's Introduction

Creating a boilerplate for React 18 in ViteJS

Editorconfig

If your are using editorconfig, this is my config:

# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

For ESLint and Prettier

npm i eslint @babel/eslint-parser @babel/preset-env @babel/preset-react prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-storybook -D

Create the files .eslintrc.js, .prettierrc.js and a file called babel.config.js.

.eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
    jest: true,
    node: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:react-hooks/recommended',
    'plugin:prettier/recommended',
    'plugin:storybook/recommended',
  ],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parser: '@babel/eslint-parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 'latest',
    sourceType: 'module',
  },
  plugins: ['react', 'prettier', 'react-hooks'],
  settings: {
    react: {
      version: 'detect',
    },
  },
  rules: {
    'react/react-in-jsx-scope': 'off',
  },
};

.prettierrc.js

module.exports = {
  arrowParens: 'always',
  bracketSpacing: true,
  endOfLine: 'lf',
  htmlWhitespaceSensitivity: 'ignore',
  insertPragma: false,
  jsxSingleQuote: false,
  printWidth: 80,
  proseWrap: 'always',
  quoteProps: 'as-needed',
  requirePragma: false,
  semi: true,
  singleQuote: true,
  tabWidth: 2,
  trailingComma: 'all',
  useTabs: false,
  vueIndentScriptAndStyle: false,
  embeddedLanguageFormatting: 'off',
};

babel.config.js

module.exports = {
  presets: [
    '@babel/preset-env',
    ['@babel/preset-react', { runtime: 'automatic' }],
  ],
};

Prop-types?

If your are using proptypes, install it:

npm i prop-types

And use:

Testing

For tests, we're going to use vitest and testing-library.

For vitest

npm i -D vitest jsdom @testing-library/react @testing-library/jest-dom

Now, in your vite.config.js:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  root: 'src',
  build: {
    outDir: '../dist',
  },
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['../.test/setup.js'],
    include: ['**/*(*.)?{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
    exclude: ['node_modules', 'dist', '.idea', '.git', '.cache'],
  },
});

For the file in /.test/setup.js, use:

import '@testing-library/jest-dom';
import 'jest-styled-components';

Ps.: I added jest-styled-components to make it easy for me. I am going to use Styled-Components. If your aren't, please comment that line out.

For Styled Components

npm i styled-components
npm i -D jest-styled-components @types/styled-components

Create a folder called styles in src. Add the files render-theme.jsx, global-styles.jsx, theme.js and a styled-theme-provider.jsx.

The file theme.js is where you should add the theme for your application, for example (the most simple and ugly theme you would ever see):

export const theme = {
  colors: {
    primary: 'red',
    secondary: 'blue',
  },
};

The global-styles.jsx is where we add the global theme for our application. Here we can add fonts, provide css reset and more.

import { createGlobalStyle } from 'styled-components';

export const GlobalStyles = createGlobalStyle`
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  html {
    font-size: 62.5%;
  }

  body {
    font-size: 1.6rem;
  }
`;

The styled-theme-provider.jsx makes things easier by providing the theme to other components.

import { ThemeProvider } from 'styled-components';
import Proptypes from 'prop-types';
import { GlobalStyles } from './global-styles';
import { theme } from './theme';

export const StyledThemeProvider = ({ children }) => {
  return (
    <ThemeProvider theme={theme}>
      <GlobalStyles />
      {children}
    </ThemeProvider>
  );
};

StyledThemeProvider.propTypes = {
  children: Proptypes.node.isRequired,
};

The file render-theme.jsx is going to be used for tests. It will provide the StyledThemeProvider so we can use styled-components in our tests.

This is the most basic version of the renderTheme function.

import { render } from '@testing-library/react';
import { StyledThemeProvider } from './styled-theme-provider';

export const renderTheme = (children) => {
  return render(<StyledThemeProvider>{children}</StyledThemeProvider>);
};

And on the main.jsx, you may want to wrap everything using StyledThemeProvider.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { StyledThemeProvider } from './styles/styled-theme-provider';
import App from './App';
import './index.css';

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <StyledThemeProvider>
      <App />
    </StyledThemeProvider>
  </React.StrictMode>,
);

Show markdown as HTML

npm install react-markdown
import ReactMarkdown from 'react-markdown';
import { TextComponent } from '../TextComponent';

<TextComponent>
  <ReactMarkdown>{html}</ReactMarkdown>,
</TextComponent>

curso-reactjs-nextjs-project-3-2022's People

Contributors

luizomf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.