Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite "adding React to existing app" #992

Closed
wants to merge 21 commits into from

Conversation

alexkrolick
Copy link
Collaborator

@alexkrolick alexkrolick commented Jun 23, 2018

Fixes #988

Rendered

@reactjs-bot
Copy link

reactjs-bot commented Jun 23, 2018

Deploy preview for reactjs ready!

Built with commit ec791eb

https://deploy-preview-992--reactjs.netlify.com

```html
<script
crossorigin
src="https://unpkg.com/react@16/umd/react.production.min.js">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make these "development" versions at first, and add a 5th step that says "Before deploying, replace the React script tags with production versions"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last step should also say something "You probably already have a minification step for your JavaScript—don't forget to minify your component files too!"

import React from 'react';
import ReactDOM from 'react-dom';
// src/widget.js
class Widget extends React.Component() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: shouldn't have parens at the end

<h1>Hello, world!</h1>,
document.getElementById('root')
<Widget />,
document.querySelector('.widget')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this still looks too much like an SPA entry point. Maybe we could render the same component a few times to different nodes? e.g. three buttons


### Development and Production Versions
```shell
npm install --global babel-cli babel-preset-react-app
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react-app preset won't work if you don't explicitly specify NODE_ENV.

However we can add an entry point like babel-preset-react-app/prod to avoid messing with env variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this won't work: Babel can't locate a global preset.

I think it'll have to be local installs.

@gaearon
Copy link
Member

gaearon commented Jun 23, 2018

I have some ideas for a larger set of changes. Will do on top of this. Thanks!

@alexkrolick
Copy link
Collaborator Author

Ok cool 😄

Copy link
Contributor

@swyxio swyxio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some comments, overall nice effort!

```js
// src/button.js

class Button extends React.Component {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to use functional components unless you have a need for classes

Copy link
Collaborator Author

@alexkrolick alexkrolick Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is familiar syntax that always works. Shouldn't have to explain function components on the install page.

(Most components are going to have state anyways - else why would you be adding React to a static page?)

);
```

This code renders into a DOM element with the id of `root`, so you need `<div id="root"></div>` somewhere in your HTML file.
### 3. Compile with Babel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need babel to compile, we can require inline, the whole point of this piece is that we can use babel in a script:

// babel in a script

<script src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>

// lower down

<script type="text/babel">

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's extremely slow - this is meant for people adding React to an actual app. If they are just trying React they should be referring to the Try React page.

* [Creating a Production Build with Browserify](/docs/optimizing-performance.html#browserify)
* [Creating a Production Build with Rollup](/docs/optimizing-performance.html#rollup)
* [Creating a Production Build with webpack](/docs/optimizing-performance.html#webpack)
You probably already have a minification step for your JavaScript - don't forget to minify your component files too!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need this, if you are using the prod version of react ( I think)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The component scripts won't minify themselves 😄
The production CDN links are minified copies of React & ReactDOM though

@alexkrolick
Copy link
Collaborator Author

alexkrolick commented Jun 25, 2018

@gaearon I like using the existing HTML page as a starting point, good idea

What do you think about using a details tag to hide the linked code samples inline, but make them expandable? I worry that relying on external links makes it hard to maintain context.

Like this

Download examples

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Add React in One Minute</title>
  </head>
  <body>

    <h2>Add React in One Minute</h2>
    <p>This page demonstrates using React with no build tooling.</p>
    <p>React is loaded as a script tag.</p>

    <!-- We will put our React component inside this div. -->
    <div class="like_button_container"></div>

    <!-- Load React. -->
    <!-- Change .development.js to .production.min.js in both tags before deployment! -->
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

    <!-- Load our React component. -->
    <script src="like_button.js"></script>

  </body>
</html>

like_button.js

'use strict';

class LikeButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { liked: false };
  }

  render() {
    if (this.state.liked) {
      return 'You liked this.';
    }

    return React.createElement(
      'button',
      { onClick: () => this.setState({ liked: true }) },
      'Like'
    );
  }
}

// Find the DOM container we defined in HTML.
let domContainer = document.querySelector('.like_button_container');

// Show the LikeButton component inside our DOM container.
ReactDOM.render(React.createElement(LikeButton), domContainer);

**Make sure you've followed the previous steps.** Then create a folder called `src` and run this terminal command:

```
npx babel --watch src --out-dir . --presets react-app/prod
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screen shot 2018-06-25 at 11 19 23 am

screen shot 2018-06-25 at 11 20 22 am

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you missed the previous steps, as it mentions? :-)

@gaearon
Copy link
Member

gaearon commented Jun 25, 2018

Superseded by #996.
Thanks for starting this

@gaearon gaearon closed this Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants