Skip to content
Merged
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
7 changes: 4 additions & 3 deletions content/home/examples/a-component-using-external-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MarkdownEditor extends React.Component {
}
}

// let mountNode = document.querySelector('#markdown')

ReactDOM.render(<MarkdownEditor />, mountNode);
ReactDOM.render(
<MarkdownEditor />,
document.getElementById('markdown-example')
);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: A Component Using External Plugins
order: 3
domid: markdown-example
---

React is flexible and provides hooks that allow you to interface with other libraries and frameworks. This example uses **remarkable**, an external Markdown library, to convert the `<textarea>`'s value in real time.
4 changes: 1 addition & 3 deletions content/home/examples/a-simple-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class HelloMessage extends React.Component {
}
}

// let mountNode = document.querySelector('#message')

ReactDOM.render(
<HelloMessage name="Taylor" />,
mountNode
document.getElementById('hello-example')
);
1 change: 1 addition & 0 deletions content/home/examples/a-simple-component.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: A Simple Component
order: 0
domid: hello-example
---

React components implement a `render()` method that takes input data and returns what to display. This example uses an XML-like syntax called JSX. Input data that is passed into the component can be accessed by `render()` via `this.props`.
Expand Down
7 changes: 4 additions & 3 deletions content/home/examples/a-stateful-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Timer extends React.Component {
}
}

// let mountNode = document.querySelector('#timer')

ReactDOM.render(<Timer />, mountNode);
ReactDOM.render(
<Timer />,
document.getElementById('timer-example')
);
1 change: 1 addition & 0 deletions content/home/examples/a-stateful-component.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: A Stateful Component
order: 1
domid: timer-example
---

In addition to taking input data (accessed via `this.props`), a component can maintain internal state data (accessed via `this.state`). When a component's state data changes, the rendered markup will be updated by re-invoking `render()`.
7 changes: 4 additions & 3 deletions content/home/examples/an-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TodoList extends React.Component {
}
}

// let mountNode = document.querySelector('#app')

ReactDOM.render(<TodoApp />, mountNode);
ReactDOM.render(
<TodoApp />,
document.getElementById('todos-example')
);
1 change: 1 addition & 0 deletions content/home/examples/an-application.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: An Application
order: 2
domid: todos-example
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perfect

---

Using `props` and `state`, we can put together a small Todo application. This example uses `state` to track the current list of items as well as the text that the user has entered. Although event handlers appear to be rendered inline, they will be collected and implemented using event delegation.
17 changes: 3 additions & 14 deletions src/components/CodeEditor/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class CodeEditor extends Component {
}

componentDidMount() {
// Initial render() will always be a no-op,
// Because the mountNode ref won't exist yet.
this._render();
Copy link
Member Author

@gaearon gaearon Jan 2, 2019

Choose a reason for hiding this comment

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

Technically this would still not work on first render if parents weren't already in the documented. I can add a setTimeout but don't care enough about this (?)

If this breaks I'm pretty sure we'd notice that fast.

}

Expand All @@ -45,7 +43,7 @@ class CodeEditor extends Component {
}

render() {
const {children} = this.props;
const {children, containerNodeID} = this.props;
const {
compiledES6,
code,
Expand Down Expand Up @@ -205,6 +203,7 @@ class CodeEditor extends Component {
<MetaTitle>Result</MetaTitle>
</div>
<div
id={containerNodeID}
css={{
padding: 10,
maxHeight: '340px !important',
Expand Down Expand Up @@ -233,7 +232,6 @@ class CodeEditor extends Component {
padding: 5,
},
}}
ref={this._setMountRef}
/>
</div>
)}
Expand All @@ -243,21 +241,16 @@ class CodeEditor extends Component {
}

_render() {
if (!this._mountNode) {
return;
}

const {compiled} = this.state;

try {
// Example code requires React, ReactDOM, and Remarkable to be within scope.
// It also requires a "mountNode" variable for ReactDOM.render()
// eslint-disable-next-line no-new-func
new Function('React', 'ReactDOM', 'Remarkable', 'mountNode', compiled)(
new Function('React', 'ReactDOM', 'Remarkable', compiled)(
React,
ReactDOM,
Remarkable,
this._mountNode,
);
} catch (error) {
console.error(error);
Expand All @@ -269,10 +262,6 @@ class CodeEditor extends Component {
}
}

_setMountRef = ref => {
this._mountNode = ref;
};

_updateState(code, showJSX = true) {
try {
let newState = {
Expand Down
8 changes: 6 additions & 2 deletions src/components/CodeExample/CodeExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CodeEditor from '../CodeEditor/CodeEditor';

class CodeExample extends Component {
render() {
const {children, code, id, loaded} = this.props;
const {children, code, id, containerNodeID, loaded} = this.props;
return (
<div
id={id}
Expand Down Expand Up @@ -58,7 +58,11 @@ class CodeExample extends Component {
{children}
</div>
)}
{loaded ? <CodeEditor code={code} /> : <h4>Loading code example...</h4>}
{loaded ? (
<CodeEditor code={code} containerNodeID={containerNodeID} />
) : (
<h4>Loading code example...</h4>
)}
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class Home extends Component {
key={index}
id={snippet.id}
code={snippet.code}
containerNodeID={node.frontmatter.domid}
loaded={babelLoaded}>
<h3 css={headingStyles}>{node.frontmatter.title}</h3>
<div dangerouslySetInnerHTML={{__html: node.html}} />
Expand Down Expand Up @@ -368,6 +369,7 @@ export const pageQuery = graphql`
}
frontmatter {
title
domid
}
html
}
Expand Down