Skip to content

Commit 13c0e48

Browse files
committed
Merge branch 'refactor-index'
2 parents f91081e + f5aaf4c commit 13c0e48

20 files changed

+651
-580
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class MarkdownEditor extends React.Component {
2+
constructor(props) {
3+
super(props);
4+
this.handleChange = this.handleChange.bind(this);
5+
this.state = { value: 'Type some *markdown* here!' };
6+
}
7+
8+
handleChange(e) {
9+
this.setState({ value: e.target.value });
10+
}
11+
12+
getRawMarkup() {
13+
const md = new Remarkable();
14+
return { __html: md.render(this.state.value) };
15+
}
16+
17+
render() {
18+
return (
19+
<div className="MarkdownEditor">
20+
<h3>Input</h3>
21+
<textarea
22+
onChange={this.handleChange}
23+
defaultValue={this.state.value}
24+
/>
25+
<h3>Output</h3>
26+
<div
27+
className="content"
28+
dangerouslySetInnerHTML={this.getRawMarkup()}
29+
/>
30+
</div>
31+
);
32+
}
33+
}
34+
35+
ReactDOM.render(<MarkdownEditor />, mountNode);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: A Component Using External Plugins
3+
order: 3
4+
---
5+
6+
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.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class HelloMessage extends React.Component {
2+
render() {
3+
return (
4+
<div>
5+
Hello {this.props.name}
6+
</div>
7+
);
8+
}
9+
}
10+
11+
ReactDOM.render(
12+
<HelloMessage name="Taylor" />,
13+
mountNode
14+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: A Simple Component
3+
order: 0
4+
---
5+
6+
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`.
7+
8+
**JSX is optional and not required to use React.** Try the [Babel REPL](http://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&code_lz=MYGwhgzhAEASCmIQHsCy8pgOb2vAHgC7wB2AJjAErxjCEB0AwsgLYAOyJph0A3gFDRoAJ1Jl4wgBQBKPoKEj4hAK7CS0SfIXQAPGQCWANwB8W7XEQo-hABb6I9NsORsHJMC3gBfM0J0B6AxMzaQBueR8ffmpaQgARAHlUelFyCU0_BCQ0DAhsXHdPAF4AIgAVMABPFGES6H9jABp5FmRlEkIAOWRxfjCgA&debug=false&circleciRepo=&evaluate=false&lineWrap=false&presets=react&targets=&version=6.26.0) to see the raw JavaScript code produced by the JSX compilation step.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Timer extends React.Component {
2+
constructor(props) {
3+
super(props);
4+
this.state = { seconds: 0 };
5+
}
6+
7+
tick() {
8+
this.setState(prevState => ({
9+
seconds: prevState.seconds + 1
10+
}));
11+
}
12+
13+
componentDidMount() {
14+
this.interval = setInterval(() => this.tick(), 1000);
15+
}
16+
17+
componentWillUnmount() {
18+
clearInterval(this.interval);
19+
}
20+
21+
render() {
22+
return (
23+
<div>
24+
Seconds: {this.state.seconds}
25+
</div>
26+
);
27+
}
28+
}
29+
30+
ReactDOM.render(<Timer />, mountNode);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: A Stateful Component
3+
order: 1
4+
---
5+
6+
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()`.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class TodoApp extends React.Component {
2+
constructor(props) {
3+
super(props);
4+
this.state = { items: [], text: '' };
5+
this.handleChange = this.handleChange.bind(this);
6+
this.handleSubmit = this.handleSubmit.bind(this);
7+
}
8+
9+
render() {
10+
return (
11+
<div>
12+
<h3>TODO</h3>
13+
<TodoList items={this.state.items} />
14+
<form onSubmit={this.handleSubmit}>
15+
<input
16+
onChange={this.handleChange}
17+
value={this.state.text}
18+
/>
19+
<button>
20+
Add #{this.state.items.length + 1}
21+
</button>
22+
</form>
23+
</div>
24+
);
25+
}
26+
27+
handleChange(e) {
28+
this.setState({ text: e.target.value });
29+
}
30+
31+
handleSubmit(e) {
32+
e.preventDefault();
33+
if (!this.state.text.length) {
34+
return;
35+
}
36+
const newItem = {
37+
text: this.state.text,
38+
id: Date.now()
39+
};
40+
this.setState(prevState => ({
41+
items: prevState.items.concat(newItem),
42+
text: ''
43+
}));
44+
}
45+
}
46+
47+
class TodoList extends React.Component {
48+
render() {
49+
return (
50+
<ul>
51+
{this.props.items.map(item => (
52+
<li key={item.id}>{item.text}</li>
53+
))}
54+
</ul>
55+
);
56+
}
57+
}
58+
59+
ReactDOM.render(<TodoApp />, mountNode);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: An Application
3+
order: 2
4+
---
5+
6+
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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Component-Based
3+
order: 1
4+
---
5+
6+
Build encapsulated components that manage their own state, then compose them to make complex UIs.
7+
8+
Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.

content/home/marketing/declarative.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Declarative
3+
order: 0
4+
---
5+
6+
React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.
7+
8+
Declarative views make your code more predictable and easier to debug.

0 commit comments

Comments
 (0)