Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codesandbox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!*
30 changes: 30 additions & 0 deletions codesandbox/examples/cra/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react",
"version": "1.0.0",
"description": "React example starter project",
"keywords": [
"react",
"starter"
],
"main": "src/index.js",
"dependencies": {
"react": "16.12.0",
"react-dom": "16.12.0",
"react-scripts": "3.0.1"
},
"devDependencies": {
"typescript": "3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
Copy link
Member

@rickhanlonii rickhanlonii Jul 11, 2020

Choose a reason for hiding this comment

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

Do we know what will happen when people browse embedded codesandboxes on unsupported browsers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, yes this is a good point. Most probably it will just be a console error, but I can catch this maybe and show a better message. By the way, which browsers do you aim to support with the runnable examples? I'll make sure they'll work in those browsers.

Copy link
Member

Choose a reason for hiding this comment

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

That's a good question, these supported browser options seem fair to me but I defer to @gaearon.

]
}
43 changes: 43 additions & 0 deletions codesandbox/examples/cra/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>

<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>

</html>
10 changes: 10 additions & 0 deletions codesandbox/examples/cra/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
12 changes: 12 additions & 0 deletions codesandbox/examples/cra/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
rootElement
);
30 changes: 30 additions & 0 deletions codesandbox/examples/simple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react",
"version": "1.0.0",
"description": "React example starter project",
"keywords": [
"react",
"starter"
],
"main": "src/index.js",
"dependencies": {
"react": "16.12.0",
"react-dom": "16.12.0",
"react-scripts": "3.0.1"
},
"devDependencies": {
"typescript": "3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Empty file.
30 changes: 30 additions & 0 deletions codesandbox/examples/umd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react",
"version": "1.0.0",
"description": "React example starter project",
"keywords": [
"react",
"starter"
],
"main": "src/index.js",
"dependencies": {
"react": "16.12.0",
"react-dom": "16.12.0",
"react-scripts": "3.0.1"
},
"devDependencies": {
"typescript": "3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
53 changes: 53 additions & 0 deletions codesandbox/examples/umd/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->

<script
crossorigin
src="https://unpkg.com/react@16/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
></script>
<title>React App</title>
</head>

<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Empty file.
13 changes: 4 additions & 9 deletions content/docs/handling-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ When using React, you generally don't need to call `addEventListener` to add lis

When you define a component using an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes), a common pattern is for an event handler to be a method on the class. For example, this `Toggle` component renders a button that lets the user toggle between "ON" and "OFF" states:

```js{6,7,10-14,18}
class Toggle extends React.Component {
```js codesandbox=cra?highlights=8,9,12,13,14,15,16,20&height=400
import React from 'react';

export default class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
Expand All @@ -84,15 +86,8 @@ class Toggle extends React.Component {
);
}
}

ReactDOM.render(
<Toggle />,
document.getElementById('root')
);
```

[**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010)

You have to be careful about the meaning of `this` in JSX callbacks. In JavaScript, class methods are not [bound](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) by default. If you forget to bind `this.handleClick` and pass it to `onClick`, `this` will be `undefined` when the function is actually called.

This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method.
Expand Down
4 changes: 1 addition & 3 deletions content/docs/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ next: introducing-jsx.html

The smallest React example looks like this:

```js
```js codesandbox=umd
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
Expand All @@ -17,8 +17,6 @@ ReactDOM.render(

It displays a heading saying "Hello, world!" on the page.

[](codepen://hello-world)

Click the link above to open an online editor. Feel free to make some changes, and see how they affect the output. Most pages in this guide will have editable examples like this one.


Expand Down
4 changes: 2 additions & 2 deletions content/docs/hooks-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ next: hooks-overview.html

*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class.

```js{4,5}
```js codesandbox=cra?highlights=4
import React, { useState } from 'react';

function Example() {
export default function App() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);

Expand Down
29 changes: 29 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ module.exports = {
resolve: 'gatsby-transformer-remark',
options: {
plugins: [
{
resolve: 'gatsby-remark-inline-codesandbox',
options: {
mode: 'iframe',
// Whether we generate the sandboxes during build-time
autoDeploy: process.env.NODE_ENV === 'production',
query: {
codemirror: 1,
fontsize: 14,
hidenavigation: 1,
editorsize: 70,
hidedevtools: 1,
},
customTemplates: {
umd: {
extends: 'file:./codesandbox/examples/umd',
entry: 'src/index.js',
},
simple: {
extends: 'file:./codesandbox/examples/simple',
entry: 'src/index.js',
},
cra: {
extends: 'file:./codesandbox/examples/cra',
entry: 'src/App.js',
},
},
},
},
'gatsby-remark-responsive-iframe',
{
resolve: 'gatsby-remark-images',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"gatsby-remark-embed-snippet": "^3.0.0",
"gatsby-remark-external-links": "^0.0.4",
"gatsby-remark-images": "^2.0.0",
"gatsby-remark-inline-codesandbox": "^0.5.2",
"gatsby-remark-prismjs": "^3.0.2",
"gatsby-remark-responsive-iframe": "^2.0.0",
"gatsby-remark-smartypants": "^2.0.0",
Expand Down
Loading