mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-23 20:23:08 +00:00
@@ -27,16 +27,14 @@ Nel resto di questo tutorial vengono illustrate le best practices, usando JSX e
|
||||
Nella maggior parte dei casi dovresti esplicitamente passare le proprietà. Ciò assicura che venga esposto soltanto un sottoinsieme dell'API interna, del cui funzionamento si è certi.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={this.props.onClick}>
|
||||
{this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={props.onClick}>
|
||||
{props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Ciao mondo!
|
||||
@@ -58,22 +56,20 @@ A volte passare manualmente ciascuna proprietà può essere noioso e fragile. In
|
||||
Elenca tutte le proprietà che desideri consumare, seguite da `...other`.
|
||||
|
||||
```javascript
|
||||
var { checked, ...other } = this.props;
|
||||
var { checked, ...other } = props;
|
||||
```
|
||||
|
||||
Ciò assicura che vengano passate tutte le proprietà TRANNE quelle che stai consumando tu stesso.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` contiene { onClick: console.log } ma non la proprietà checked
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` contiene { onClick: console.log } ma non la proprietà checked
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Ciao mondo!
|
||||
@@ -89,15 +85,13 @@ ReactDOM.render(
|
||||
Usa sempre il pattern di destrutturazione quando trasferisci altre proprietà sconosciute in `other`.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// ANTI-PATTERN: `checked` sarebbe passato al componente interno
|
||||
return (
|
||||
<div {...this.props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// ANTI-PATTERN: `checked` sarebbe passato al componente interno
|
||||
return (
|
||||
<div {...props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Consumare e Trasferire la Stessa Proprietà
|
||||
@@ -105,23 +99,21 @@ var FancyCheckbox = React.createClass({
|
||||
Se il tuo componente desidera consumare una proprietà, ma anche passarla ad altri, puoi passarla esplicitamente mediante `checked={checked}`. Questo è preferibile a passare l'intero oggetto `this.props` dal momento che è più facile effettuarne il linting e il refactoring.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, title, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, title, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> NOTA:
|
||||
@@ -150,14 +142,12 @@ z; // { a: 3, b: 4 }
|
||||
Se non usi JSX, puoi usare una libreria per ottenere il medesimo pattern. Underscore supporta `_.omit` per omettere delle proprietà ed `_.extend` per copiare le proprietà in un nuovo oggetto.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var checked = this.props.checked;
|
||||
var other = _.omit(this.props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var checked = props.checked;
|
||||
var other = _.omit(props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -29,16 +29,14 @@ React.createElement(Component, Object.assign({}, this.props, { more: 'values' })
|
||||
ほとんどの場合、プロパティを明確に子要素に渡すべきです。それは、内部のAPIのサブセットだけを外に出していることと、認識しているプロパティが動作することを保証します。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={this.props.onClick}>
|
||||
{this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={props.onClick}>
|
||||
{props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Hello world!
|
||||
@@ -59,22 +57,20 @@ ReactDOM.render(
|
||||
以下のように `...other` を使うことで、使いたいプロパティを一覧にすることができます。
|
||||
|
||||
```javascript
|
||||
var { checked, ...other } = this.props;
|
||||
var { checked, ...other } = props;
|
||||
```
|
||||
|
||||
これは、自分で指定したものは 除き 、全てのpropsを渡すことを保証します。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` は { onClick: console.log } を含みますが、 checked プロパティは含みません。
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` は { onClick: console.log } を含みますが、 checked プロパティは含みません。
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Hello world!
|
||||
@@ -89,15 +85,13 @@ ReactDOM.render(
|
||||
未知の `other` propsを移譲する際には、分割代入パターンを常に使ってください。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// アンチパターン: `checked` が内部のコンポーネントに渡されます。
|
||||
return (
|
||||
<div {...this.props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// アンチパターン: `checked` が内部のコンポーネントに渡されます。
|
||||
return (
|
||||
<div {...props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 同じpropを使い、移譲する
|
||||
@@ -105,23 +99,21 @@ var FancyCheckbox = React.createClass({
|
||||
コンポーネントがプロパティを使うだけでなく、子要素に渡したい場合は、明確に `checked={checked}` と記述することで再度渡すことができます。 `this.props` オブジェクトで全てを渡すほうが、リファクタリングやチェックをしやすいので好ましいです。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, title, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, title, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> 注意:
|
||||
@@ -148,14 +140,12 @@ z; // { a: 3, b: 4 }
|
||||
JSXを使わない際には、同じパターンを行うライブラリを使うことができます。Underscoreでは、 `_.omit` を使ってプロパティをフィルタしたり、 `_.extend` を使って新しいオブジェクトにプロパティをコピーしたりできます。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var checked = this.props.checked;
|
||||
var other = _.omit(this.props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var checked = props.checked;
|
||||
var other = _.omit(props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -27,16 +27,14 @@ React.createElement(Component, Object.assign({}, this.props, { more: 'values' })
|
||||
대부분의 경우 명시적으로 프로퍼티를 아래로 전달해야 합니다. 이는 동작을 확신하는 내부 API의 일부만 공개하도록 합니다.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={this.props.onClick}>
|
||||
{this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={props.onClick}>
|
||||
{props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
세상아 안녕!
|
||||
@@ -59,22 +57,20 @@ ReactDOM.render(
|
||||
소비할 프로퍼티들을 나열하고, 그 뒤에 `...other`를 넣습니다.
|
||||
|
||||
```javascript
|
||||
var { checked, ...other } = this.props;
|
||||
var { checked, ...other } = props;
|
||||
```
|
||||
|
||||
이는 지금 소비한 props를 *제외한* 나머지를 아래로 전달합니다.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other`에는 { onClick: console.log }가 포함되지만 checked 프로퍼티는 제외됩니다
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other`에는 { onClick: console.log }가 포함되지만 checked 프로퍼티는 제외됩니다
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
세상아 안녕!
|
||||
@@ -90,15 +86,13 @@ ReactDOM.render(
|
||||
미상의 `other` props을 전달할 때는 항상 구조 해체 패턴을 사용하세요.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// 반례: `checked` 또한 내부 컴포넌트로 전달될 것입니다
|
||||
return (
|
||||
<div {...this.props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// 반례: `checked` 또한 내부 컴포넌트로 전달될 것입니다
|
||||
return (
|
||||
<div {...props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 같은 Prop을 소비하고 전달하기
|
||||
@@ -106,23 +100,21 @@ var FancyCheckbox = React.createClass({
|
||||
컴포넌트가 프로퍼티를 사용하지만 계속 넘기길 원한다면, `checked={checked}`처럼 명시적으로 다시 넘길 수 있습니다. 리팩토링과 린트(lint)하기가 더 쉬우므로 이 방식이 `this.props` 객체 전부를 넘기는 것보다 낫습니다.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, title, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, title, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> 주의:
|
||||
@@ -152,14 +144,12 @@ z; // { a: 3, b: 4 }
|
||||
JSX를 사용하지 않는다면 라이브러리를 사용해 같은 패턴을 쓸 수 있습니다. Underscore에서는 `_.omit`을 사용해 특정 프로퍼티를 제외하거나 `_.extend`를 사용해 새로운 객체로 프로퍼티를 복사할 수 있습니다.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var checked = this.props.checked;
|
||||
var other = _.omit(this.props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var checked = props.checked;
|
||||
var other = _.omit(props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -27,16 +27,14 @@ The rest of this tutorial explains best practices. It uses JSX and experimental
|
||||
Most of the time you should explicitly pass the properties down. This ensures that you only expose a subset of the inner API, one that you know will work.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={this.props.onClick}>
|
||||
{this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={props.onClick}>
|
||||
{props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Hello world!
|
||||
@@ -58,22 +56,20 @@ Sometimes it's fragile and tedious to pass every property along. In that case yo
|
||||
List out all the properties that you would like to consume, followed by `...other`.
|
||||
|
||||
```javascript
|
||||
var { checked, ...other } = this.props;
|
||||
var { checked, ...other } = props;
|
||||
```
|
||||
|
||||
This ensures that you pass down all the props EXCEPT the ones you're consuming yourself.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` contains { onClick: console.log } but not the checked property
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` contains { onClick: console.log } but not the checked property
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Hello world!
|
||||
@@ -89,39 +85,35 @@ ReactDOM.render(
|
||||
Always use the destructuring pattern when transferring unknown `other` props.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// ANTI-PATTERN: `checked` would be passed down to the inner component
|
||||
return (
|
||||
<div {...this.props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// ANTI-PATTERN: `checked` would be passed down to the inner component
|
||||
return (
|
||||
<div {...props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Consuming and Transferring the Same Prop
|
||||
|
||||
If your component wants to consume a property but also wants to pass it along, you can repass it explicitly with `checked={checked}`. This is preferable to passing the full `this.props` object since it's easier to refactor and lint.
|
||||
If your component wants to consume a property but also wants to pass it along, you can repass it explicitly with `checked={checked}`. This is preferable to passing the full `props` object since it's easier to refactor and lint.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, title, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, title, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> NOTE:
|
||||
@@ -150,14 +142,12 @@ z; // { a: 3, b: 4 }
|
||||
If you don't use JSX, you can use a library to achieve the same pattern. Underscore supports `_.omit` to filter out properties and `_.extend` to copy properties onto a new object.
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var checked = this.props.checked;
|
||||
var other = _.omit(this.props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var checked = props.checked;
|
||||
var other = _.omit(props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -27,16 +27,14 @@ React.createElement(Component, Object.assign({}, this.props, { more: 'values' })
|
||||
大部分情况下你应该显式地向下传递 props。这样可以确保只公开你认为是安全的内部 API 的子集。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={this.props.onClick}>
|
||||
{this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
<div className={fancyClass} onClick={props.onClick}>
|
||||
{props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Hello world!
|
||||
@@ -58,22 +56,20 @@ ReactDOM.render(
|
||||
列出所有要当前使用的属性,后面跟着 `...other`。
|
||||
|
||||
```javascript
|
||||
var { checked, ...other } = this.props;
|
||||
var { checked, ...other } = props;
|
||||
```
|
||||
|
||||
这样能确保把所有 props 传下去,*除了* 那些已经被使用了的。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` 包含 { onClick: console.log } 但 checked 属性除外
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// `other` 包含 { onClick: console.log } 但 checked 属性除外
|
||||
return (
|
||||
<div {...other} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
ReactDOM.render(
|
||||
<FancyCheckbox checked={true} onClick={console.log.bind(console)}>
|
||||
Hello world!
|
||||
@@ -89,15 +85,13 @@ ReactDOM.render(
|
||||
在传递这些未知的 `other` 属性时,要经常使用解构赋值模式。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var fancyClass = this.props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// 反模式:`checked` 会被传到里面的组件里
|
||||
return (
|
||||
<div {...this.props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
// 反模式:`checked` 会被传到里面的组件里
|
||||
return (
|
||||
<div {...props} className={fancyClass} />
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 使用和传递同一个 Prop
|
||||
@@ -105,23 +99,21 @@ var FancyCheckbox = React.createClass({
|
||||
如果组件需要使用一个属性又要往下传递,可以直接使用 `checked={checked}` 再传一次。这样做比传整个 `this.props` 对象要好,因为更利于重构和语法检查。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var { checked, title, ...other } = this.props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var { checked, title, ...other } = props;
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
var fancyTitle = checked ? 'X ' + title : 'O ' + title;
|
||||
return (
|
||||
<label>
|
||||
<input {...other}
|
||||
checked={checked}
|
||||
className={fancyClass}
|
||||
type="checkbox"
|
||||
/>
|
||||
{fancyTitle}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> 注意:
|
||||
@@ -150,14 +142,12 @@ z; // { a: 3, b: 4 }
|
||||
如果不使用 JSX,可以使用一些库来实现相同效果。Underscore 提供 `_.omit` 来过滤属性,`_.extend` 复制属性到新的对象。
|
||||
|
||||
```javascript
|
||||
var FancyCheckbox = React.createClass({
|
||||
render: function() {
|
||||
var checked = this.props.checked;
|
||||
var other = _.omit(this.props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
});
|
||||
function FancyCheckbox(props) {
|
||||
var checked = props.checked;
|
||||
var other = _.omit(props, 'checked');
|
||||
var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
|
||||
return (
|
||||
React.DOM.div(_.extend({}, other, { className: fancyClass }))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -13,23 +13,21 @@ next: expose-component-functions-ko-KR.html
|
||||
`GroceryList` 컴포넌트가 배열로 생성된 아이템 목록을 가지고 있다고 해봅시다. 목록의 아이템이 클릭되면 아이템의 이름이 보이길 원할 겁니다:
|
||||
|
||||
```js
|
||||
var GroceryList = React.createClass({
|
||||
handleClick: function(i) {
|
||||
console.log('클릭한 아이템: ' + this.props.items[i]);
|
||||
},
|
||||
var handleClick = function(i, props) {
|
||||
console.log('클릭한 아이템: ' + props.items[i]);
|
||||
}
|
||||
|
||||
render: function() {
|
||||
return (
|
||||
<div>
|
||||
{this.props.items.map(function(item, i) {
|
||||
return (
|
||||
<div onClick={this.handleClick.bind(this, i)} key={i}>{item}</div>
|
||||
);
|
||||
}, this)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function GroceryList(props) {
|
||||
return (
|
||||
<div>
|
||||
{props.items.map(function(item, i) {
|
||||
return (
|
||||
<div onClick={handleClick.bind(this, i, props)} key={i}>{item}</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
ReactDOM.render(
|
||||
<GroceryList items={['사과', '바나나', '크랜베리']} />, mountNode
|
||||
|
||||
@@ -13,23 +13,21 @@ For child-parent communication:
|
||||
Say your `GroceryList` component has a list of items generated through an array. When a list item is clicked, you want to display its name:
|
||||
|
||||
```js
|
||||
var GroceryList = React.createClass({
|
||||
handleClick: function(i) {
|
||||
console.log('You clicked: ' + this.props.items[i]);
|
||||
},
|
||||
var handleClick = function(i, props) {
|
||||
console.log('You clicked: ' + props.items[i]);
|
||||
}
|
||||
|
||||
render: function() {
|
||||
return (
|
||||
<div>
|
||||
{this.props.items.map(function(item, i) {
|
||||
return (
|
||||
<div onClick={this.handleClick.bind(this, i)} key={i}>{item}</div>
|
||||
);
|
||||
}, this)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
function GroceryList(props) {
|
||||
return (
|
||||
<div>
|
||||
{props.items.map(function(item, i) {
|
||||
return (
|
||||
<div onClick={handleClick.bind(this, i, props)} key={i}>{item}</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
ReactDOM.render(
|
||||
<GroceryList items={['Apple', 'Banana', 'Cranberry']} />, mountNode
|
||||
|
||||
Reference in New Issue
Block a user