React.js

React.js

2022/06/11 (增加連結)

簡介

React 是一個專注於 UI(View)的 JavaScript 函式庫(Library),利用純JavaScript在前端產生HTML(而不是由後端產生)。但是,React並沒有提供css及對應的js,所以,還是需要採用如Material-UI、Bootstrap或Semantic-UI的UI架構。

程式範例請詳見:

基礎教學

const App = () => (

<div>

<Route exact path="/" component={Home}/>

</div>

)

      • Redux

JSX

const headline = (

<h1>

{ age >= drinkingAge ? 'Buy Drink' : 'Do Teen Stuff' }

</h1>

);

render() {

return (

<div>

<h1>List of todos</h1>

{ console.log(this.props.todos) }

</div>

);

}

進階教學

重要資源

  • React admin

    • ES6

    • React

    • Material Design

  • React Starter Kit

    • isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

    • react-starter-kit-example

      • eGroup 團隊的 React Starter Kit example,這個example包括

        • redux

        • redux-observable

        • redux-action

        • semantic-ui-react

        • react-router-dom

        • normalizr

        • immutable

        • react-intl

工具

  • React Team推薦的工具 (只列部份)

  • Top 10 ReactJS Tools used by the Most Successful Developers

  • 10 Productivity Tools for React Developers in 2020

    • ESLint

    • Bit

    • Create React App

    • React Cosmos

    • React Toolbox

    • React Bootstrap

    • React Slingshot

    • Reactide

    • React Testing Library

    • Storybook

  • The React Developer Tools You Should Know in 2019

    • Component Libraries

      • React Material-UI, Grommet 2.0, React Bootstrap, Reactstrap, Prime React, Elemental-UI, Semantic-UI & more.

    • Styling Components

    • State Management

      • Among the popular tools, you can find Unstated, as well as the re-rise of Mobx which incorporates React’s encapsulated-components paradigm. It also reviews the new Context-API which is a promising alternative to Redux.

    • Testing

      • The 2018 “State of JS” survey lists the most in-demand testing frameworks looking forward into 2019, with popular testing frameworks such as Jest, Mocha, Karma and more to make TDD simpler on the component level.

    • Kick-Starters and Boilerplates

      • With the release of create-react-app 2.0 it seems like this React app boilerplate remains the most popular and recommended projects out there. However, it’s also worth taking a look at projects like React-Boilerplate and React Slingshot which provide an alternative for different use cases.

    • React Hooks and Context-API

    • React Static-site Generators

      • React brought a new era into the composition of component-based static websites, with new frameworks like Gatsby and React static.

  • 5 Tools For Faster Development In React

    1. Bit (community版:免費)

    2. StoryBook / Styleguidist

    3. React devTools

    4. Redux devTools

    5. Boilerplates & Kick-Starters

      • Are React Boilerplates Obsolete?

        • So what are the alternatives to using a boilerplate?

          • Set up the project by yourself.

          • Use create-react-app

            • If you are working on a client-side rendered application that doesn’t require webpack customization, I would recommend that you use create-react-app

          • Use create-next-app

            • If you need custom build configurations, or you need to support server-side rendering or static site rendering, then go with create-next-app.

      • Create React App (50k stars) (推薦)

      • React Boilerplate (18k stars)

      • React Slingshot (8.5k stars)

User Interface

    1. Material-UI (Google Material Design)

    2. React-Bootstrap (bootstrap 3, will be ready for bootstrap 4)

    3. Grommet

    4. Ant Design React

    5. Blueprint

    6. Reactstrap (for bootstrap 4)

    7. React toolbox (Google Material Design)

    8. React Desktop

    9. Semantic UI React

    10. Onsen UI

    11. Rebass

    12. Elemental UI

    13. Belle

    14. Prime React

    15. Atlaskit

    16. Fabric (in Typescript)

    17. Carbon Components

    18. Khan React Components

    19. Gestalt by Pinterest

    20. Material Components Web

    21. React MD (Google Material Design)

    22. React Foundation

    23. React Virtualized

State Management

With Hooks

Before Hooks

Redux

Redux & Hooks

  • Hooks (React Redux doc)

  • React | 在 Hooks 中以 useContext 與 useReducer 實現 Redux

  • React Redux | 小孩子才做選擇! Hooks 和 Redux 我全都要!

    • React Redux

      • 安裝或更新至 v7.1.0

      • Reducer & Store

    • useSelector

      • Component 不需要再經過 Connect 了!

    • useDispatch

    • useStore

  • React Hooks(三):Redux-React-Hook

    • 所以一直有意見認為React Hooks可以取代Redux,其實是捉錯用神,React Hooks最能夠取代的,其實是react-redux,也就是React與Redux相連的部份。而Redux本身,只是為前端代碼提供結構。情況與React Context很相似,當React Context一推出時,亦有聲音認為Context API將可取代Redux,其實最新版本的Redux,正正是基於Context API所開發,只有將Redux的核心概念,包括action、reducer、state等概念引入React,才可以真正取代Redux。

  • Stop Asking if React Hooks Replace Redux

    • Redux is and has always been optional!

    • React hooks and Redux aren’t trying to solve the same problems!

      • Redux is a state management library. Hooks are part of a recent update to React and let you write functional components that do everything you used to need a class component to do.

      • React hooks were developed for three main reasons:

        • It’s difficult to reuse logic between class components.

        • Lifecycle methods often contain a confusing mix of unrelated logic.

        • Classes can be difficult for both machines (and humans) to understand.

      • That being said, React hooks do give you a few new options for managing application state. Notably, the methods useState, useReducer, and useContext give you new ways to maintain your state that are arguably better and more organized than the options React gave us before hooks.

    • React hooks don’t let your React app do anything it couldn’t do before!

    • React-Redux has its own hooks, too!

    • There’s no need to frame React hooks and Redux as technologies in competition.

  • How I Eliminated Redux Boilerplate with Hooks-for-Redux

  • Refactoring Redux into React Hooks: Redux is awesome and you could still use it. But do you need it for all your features?

    • use Hooks

      • useSelector

    • Use context to deliver one change to multiple consumers

  • Add Redux to Your React App with React Hooks

Suspense (React v16.6)

Server-side rendering

Static Site Generators

Infrastructure-components

API

Airtable

fetch

Airtable package

Django

Firebase

Authetication

Google

Google Analytics

Google Map

Google Sign-In

Google Spreadsheet

Hot reload

SEO

  • How To Boost the SEO of Your React App

    • The Problems With React

      • Problem 1: Single URL for all pages

      • Problem 2: Single meta tag

      • Problem 3: All is JavaScript

    • If you really care about SEO, then you should consider building an isomorphic application.

      • Next.js and Gatsby are two great options to consider for this purpose.

    • But I Don’t Want To Use Anything Else

      • Step 1: Solving the single-URL problem

        • There is an awesome library named react-router that helps us to render content based on a URL.

      • Step 2: Solving the meta tag problem

        • Now we will use another great library named react-helmet.

      • Step 3: Solving the JavaScript problem

        • For this, we can use a service named Prerender.io.

  • ReactHelmet | Enhance the SEO of your React App

  • Easily Generate Meta Tags for Your React App (with react-helmet)

Table

Security

Tips & Experiences

Styled Components

Typescript

Toolchain

Lint

JSX & Babel

Babel

Webpack & Babel

Webpack

WebAssembly

React Native for Web

PWAs

JSON server

版本 & 新功能

常見問題

  • 為什麼會一直說package.json裡有unexpected string?

    • 先利用JSONLint檢查格式

    • 可能是package-lock.json有問題,先刪除package-lock.json,再執行

npm install --package-lock