Skip to content

Rewrite "adding React to existing app" - #992

Closed
alexkrolick wants to merge 21 commits into
reactjs:masterfrom
alexkrolick:add-existing
Closed

Rewrite "adding React to existing app"#992
alexkrolick wants to merge 21 commits into
reactjs:masterfrom
alexkrolick:add-existing

Conversation

@alexkrolick

@alexkrolickalexkrolick commented Jun 23, 2018

Copy link
Copy Markdown
Collaborator

@reactjs-bot

reactjs-bot commented Jun 23, 2018

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

@alexkrolick

Copy link
Copy Markdown
CollaboratorAuthor

Ok cool 😄

@swyxioswyxio left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

@alexkrolickalexkrolickJun 23, 2018

Copy link
Copy Markdown
CollaboratorAuthor

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
Copy Markdown
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
Copy Markdown
CollaboratorAuthor

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
Copy Markdown
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
Copy Markdown
CollaboratorAuthor

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

alexkrolick commented Jun 25, 2018

Copy link
Copy Markdown
CollaboratorAuthor

@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><metacharset="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. --><divclass="like_button_container"></div><!-- Load React. --><!-- Change .development.js to .production.min.js in both tags before deployment! --><scriptsrc="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script><scriptsrc="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script><!-- Load our React component. --><scriptsrc="like_button.js"></script></body></html>

like_button.js

'use strict';classLikeButtonextendsReact.Component{constructor(props){super(props);this.state={liked: false};}render(){if(this.state.liked){return'You liked this.';}returnReact.createElement('button',{onClick: ()=>this.setState({liked: true})},'Like');}}// Find the DOM container we defined in HTML.letdomContainer=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
Copy Markdown
CollaboratorAuthor

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
Copy Markdown
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
Copy Markdown
Member

Superseded by #996.
Thanks for starting this

@gaearongaearon closed this Jun 25, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@alexkrolick@reactjs-bot@gaearon@swyxio@facebook-github-bot