Update
This commit is contained in:
parent
d95efb54c5
commit
016c36bb96
69
react.md
69
react.md
|
@ -42,6 +42,8 @@ function MyComponent ({ name }) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Functional components have no state. Also, their `props` are passed as the first parameter to a function.
|
||||||
|
|
||||||
### Nesting
|
### Nesting
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
|
@ -93,48 +95,59 @@ These are methods available for `Component` instances. See [Component API](http:
|
||||||
|
|
||||||
Methods and properties you can override. See [component specs](http://facebook.github.io/react/docs/component-specs.html).
|
Methods and properties you can override. See [component specs](http://facebook.github.io/react/docs/component-specs.html).
|
||||||
|
|
||||||
### States & properties
|
### Properties
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<MyComponent fullscreen={true} />
|
<Video fullscreen={true} />
|
||||||
```
|
```
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
// props
|
class Video extends React.Component {
|
||||||
this.props.fullscreen //=> true
|
render () {
|
||||||
|
this.props.fullscreen
|
||||||
// state
|
/* ... */
|
||||||
this.setState({ username: 'rstacruz' });
|
}
|
||||||
this.replaceState({ ... });
|
|
||||||
this.state.username //=> 'rstacruz'
|
|
||||||
```
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
render: function () {
|
|
||||||
return <div className={this.props.fullscreen ? 'full' : ''}>
|
|
||||||
Welcome, {this.state.username}
|
|
||||||
</div>;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Use [props](https://facebook.github.io/react/docs/tutorial.html#using-props) (`this.props`) to access parameters passed from the parent.
|
Use [props](https://facebook.github.io/react/docs/tutorial.html#using-props) (`this.props`) to access parameters passed from the parent.
|
||||||
Use [states](https://facebook.github.io/react/docs/tutorial.html#reactive-state) (`this.state`) to manage dynamic data.
|
|
||||||
|
|
||||||
### Setting defaults
|
### States
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
React.createClass({
|
this.setState({ username: 'rstacruz' })
|
||||||
getInitialState: function () {
|
this.replaceState({ ... })
|
||||||
return { comments: [] };
|
|
||||||
},
|
|
||||||
|
|
||||||
getDefaultProps: function () {
|
|
||||||
return { name: "Hello" };
|
|
||||||
}
|
|
||||||
);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Pre-populates `this.state.comments` and `this.props.name`.
|
```jsx
|
||||||
|
render () {
|
||||||
|
this.state.username
|
||||||
|
/* ... */
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Use [states](https://facebook.github.io/react/docs/tutorial.html#reactive-state) (`this.state`) to manage dynamic data.
|
||||||
|
|
||||||
|
### Setting default props
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
class Hello extends React.Component {
|
||||||
|
constructor (props) {
|
||||||
|
super({ shown: true, ...props })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Setting default state
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
class Hello extends React.Component {
|
||||||
|
constructor (props) {
|
||||||
|
super(props)
|
||||||
|
this.state = { visible: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Lifecycle
|
Lifecycle
|
||||||
---------
|
---------
|
||||||
|
|
Loading…
Reference in New Issue