Skip to content

Commit 45cdfc1

Browse files
deblasisLucaBlackDragon
authored andcommitted
Translation of the Page: Composition vs Inheritance (#113)
* composition-vs-inheritance: done used CodeSandox as it's happening upstream: reactjs/react.dev#913 ...which allows uploading CSS (currently we can't with CodePen) * Prettier fix/offsets missed on 1 file * Update content/docs/composition-vs-inheritance.md Co-Authored-By: deblasis <[email protected]>
1 parent 2fd0303 commit 45cdfc1

File tree

10 files changed

+314
-79
lines changed

10 files changed

+314
-79
lines changed
Lines changed: 73 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,127 @@
11
---
22
id: composition-vs-inheritance
3-
title: Composition vs Inheritance
3+
title: Composizione vs Ereditarità
44
permalink: docs/composition-vs-inheritance.html
55
redirect_from:
66
- "docs/multiple-components.html"
77
prev: lifting-state-up.html
88
next: thinking-in-react.html
99
---
1010

11-
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
11+
React ha un potente modello di composizione, raccomandiamo che lo si usi in alternativa all'ereditarietà per riutilizzare codice tra componenti.
1212

13-
In this section, we will consider a few problems where developers new to React often reach for inheritance, and show how we can solve them with composition.
13+
In questa sezione, considereremo alcuni problemi nei quali gli sviluppatori che sono ancora agli inizi in React utilizzano l'ereditarietà, mostreremo come si possa invece risolverli con la composizione.
1414

15-
## Containment {#containment}
15+
## Contentimento {#containment}
1616

17-
Some components don't know their children ahead of time. This is especially common for components like `Sidebar` or `Dialog` that represent generic "boxes".
17+
Esistono componenti che si comportano da contenitori per altri componenti, non possono quindi sapere a priori quali componenti avranno come figli. Si pensi ad esempio a `Sidebar` (barra laterale) oppure `Dialog` (finestra di dialogo) che rappresentano "scatole" generiche.
1818

19-
We recommend that such components use the special `children` prop to pass children elements directly into their output:
19+
Raccomandiamo che questi componenti facciano uso della prop speciale `children` per passare elementi figli direttamente nell'output:
2020

2121
```js{4}
22-
function FancyBorder(props) {
22+
function BordoFigo(props) {
2323
return (
24-
<div className={'FancyBorder FancyBorder-' + props.color}>
24+
<div className={'BordoFigo BordoFigo-' + props.colore}>
2525
{props.children}
2626
</div>
2727
);
2828
}
2929
```
3030

31-
This lets other components pass arbitrary children to them by nesting the JSX:
31+
Ciò permette di passare componenti figli arbitrariamente annidandoli nel codice JSX:
3232

33-
```js{4-9}
34-
function WelcomeDialog() {
33+
```js{4-8}
34+
function FinestraBenvenuto() {
3535
return (
36-
<FancyBorder color="blue">
37-
<h1 className="Dialog-title">
38-
Welcome
39-
</h1>
40-
<p className="Dialog-message">
41-
Thank you for visiting our spacecraft!
36+
<BordoFigo colore="blue">
37+
<h1 className="Finestra-titolo">Benvenuto/a!</h1>
38+
<p className="Finestra-messaggio">
39+
Ti ringraziamo per questa tua visita nella nostra
40+
nave spaziale!
4241
</p>
43-
</FancyBorder>
42+
</BordoFigo>
4443
);
4544
}
4645
```
4746

48-
**[Try it on CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
47+
**[Prova su CodeSandbox](codesandbox://composition-vs-inheritance/1.js,composition-vs-inheritance/1.css)**
4948

50-
Anything inside the `<FancyBorder>` JSX tag gets passed into the `FancyBorder` component as a `children` prop. Since `FancyBorder` renders `{props.children}` inside a `<div>`, the passed elements appear in the final output.
49+
Il contenuto del tag JSX `<BordoFigo>` viene passato nel componente `BordoFigo` come prop `children`. Dato che `BordoFigo` renderizza `{props.children}` all'interndo di un `<div>`, gli elementi passati appaiono nell'output finale.
5150

52-
While this is less common, sometimes you might need multiple "holes" in a component. In such cases you may come up with your own convention instead of using `children`:
51+
Anche se si tratta di un approccio meno comune, a volte potresti ritrovarti ad aver bisongno di più di un "buco" all'interno di un componente. In questi casi potresti creare una tua convenzione invece di ricorrere all'uso di `children`:
5352

54-
```js{5,8,18,21}
55-
function SplitPane(props) {
53+
```js{5,7,14}
54+
function Pannello(props) {
5655
return (
57-
<div className="SplitPane">
58-
<div className="SplitPane-left">
59-
{props.left}
60-
</div>
61-
<div className="SplitPane-right">
62-
{props.right}
56+
<div className="Pannello">
57+
<div className="Pannello-sinistra">
58+
{props.sinistra}
6359
</div>
60+
<div className="Pannello-destra">{props.destra}</div>
6461
</div>
6562
);
6663
}
6764
6865
function App() {
6966
return (
70-
<SplitPane
71-
left={
72-
<Contacts />
73-
}
74-
right={
75-
<Chat />
76-
} />
67+
<Pannello sinistra={<Contatti />} destra={<Chat />} />
7768
);
7869
}
7970
```
8071

81-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)
72+
**[Prova su CodeSandbox](codesandbox://composition-vs-inheritance/2.js,composition-vs-inheritance/2.css)**
73+
8274

83-
React elements like `<Contacts />` and `<Chat />` are just objects, so you can pass them as props like any other data. This approach may remind you of "slots" in other libraries but there are no limitations on what you can pass as props in React.
75+
Gli elementi React `<Contatti />` e `<Chat />` sono dei semplici oggetti, quindi puoi passarli come props esattamente come faresti con altri dati. Questo approccio potrebbe ricordarti il concetto di "slots" in altre librerie, ma non ci sono limitazioni su cosa puoi passare come props in React.
8476

85-
## Specialization {#specialization}
77+
## Specializzazioni {#specialization}
8678

87-
Sometimes we think about components as being "special cases" of other components. For example, we might say that a `WelcomeDialog` is a special case of `Dialog`.
79+
A volte pensiamo ai componenti come se fossero "casi speciali" di altri componenti. Ad esempio, potremmo dire che `FinestraBenvenuto` è una specializzazione di `Finestra`.
8880

89-
In React, this is also achieved by composition, where a more "specific" component renders a more "generic" one and configures it with props:
81+
In React, ciò si ottiene mediante composizione, dove componenti più "specifici" renderizzano la versione più "generica" configurandola mediante props:
9082

91-
```js{5,8,16-18}
92-
function Dialog(props) {
83+
```js{4,6,15,16}
84+
function Finestra(props) {
9385
return (
94-
<FancyBorder color="blue">
95-
<h1 className="Dialog-title">
96-
{props.title}
97-
</h1>
98-
<p className="Dialog-message">
99-
{props.message}
86+
<BordoFigo colore="blue">
87+
<h1 className="Finestra-title">{props.titolo}</h1>
88+
<p className="Finestra-messaggio">
89+
{props.messaggio}
10090
</p>
101-
</FancyBorder>
91+
</BordoFigo>
10292
);
10393
}
10494
105-
function WelcomeDialog() {
95+
function FinestraBenvenuto() {
10696
return (
107-
<Dialog
108-
title="Welcome"
109-
message="Thank you for visiting our spacecraft!" />
97+
<Finestra
98+
titolo="Benvenuto/a!"
99+
messaggio="Ti ringraziamo per questa tua visita nella nostra
100+
nave spaziale!"
101+
/>
110102
);
111103
}
112104
```
113105

114-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
106+
**[Prova su CodeSandbox](codesandbox://composition-vs-inheritance/3.js,composition-vs-inheritance/3.css)**
115107

116-
Composition works equally well for components defined as classes:
117108

118-
```js{10,27-31}
119-
function Dialog(props) {
109+
La composizione funziona ugualmente bene per i componenti definiti come classi:
110+
111+
```js{8,26-32}
112+
function Finestra(props) {
120113
return (
121-
<FancyBorder color="blue">
122-
<h1 className="Dialog-title">
123-
{props.title}
124-
</h1>
125-
<p className="Dialog-message">
126-
{props.message}
114+
<BordoFigo colore="blue">
115+
<h1 className="Finestra-titolo">{props.titolo}</h1>
116+
<p className="Finestra-messaggio">
117+
{props.messaggio}
127118
</p>
128119
{props.children}
129-
</FancyBorder>
120+
</BordoFigo>
130121
);
131122
}
132123
133-
class SignUpDialog extends React.Component {
124+
class FinestraRegistrazione extends React.Component {
134125
constructor(props) {
135126
super(props);
136127
this.handleChange = this.handleChange.bind(this);
@@ -140,14 +131,17 @@ class SignUpDialog extends React.Component {
140131
141132
render() {
142133
return (
143-
<Dialog title="Mars Exploration Program"
144-
message="How should we refer to you?">
145-
<input value={this.state.login}
146-
onChange={this.handleChange} />
134+
<Finestra
135+
titolo="Programma di Esplorazione di Marte"
136+
messaggio="Qual'è il tuo nome?">
137+
<input
138+
value={this.state.login}
139+
onChange={this.handleChange}
140+
/>
147141
<button onClick={this.handleSignUp}>
148-
Sign Me Up!
142+
Registrami!
149143
</button>
150-
</Dialog>
144+
</Finestra>
151145
);
152146
}
153147
@@ -156,17 +150,18 @@ class SignUpDialog extends React.Component {
156150
}
157151
158152
handleSignUp() {
159-
alert(`Welcome aboard, ${this.state.login}!`);
153+
alert(`Benvenuto/a a bordo, ${this.state.login}!`);
160154
}
161155
}
162156
```
163157

164-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
158+
**[Prova su CodeSandbox](codesandbox://composition-vs-inheritance/4.js,composition-vs-inheritance/4.css)**
159+
165160

166-
## So What About Inheritance? {#so-what-about-inheritance}
161+
## E per quanto riguarda l'ereditarietà? {#so-what-about-inheritance}
167162

168-
At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.
163+
In Facebook, usiamo React in migliaia di componenti ma non abbiamo mai avuto alcun caso in cui sarebbe raccomandabile utilizzare gerarchie di ereditarietà per i componenti.
169164

170-
Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.
165+
Le props e la composizione ti offrono tutta la flessibilità di cui hai bisogno per personalizzare l'aspetto ed il comportamento di un componente in modo esplicito e sicuro. Ricorda che i componenti possono accettare props arbitrarie, inclusi valori primitivi, elementi React o funzioni.
171166

172-
If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.
167+
Se vuoi riutilizzare le funzionalità non strettamente legate alla UI tra componenti, suggeriamo di estrarre tali logiche all'interno di un modulo JavaScript separato. I componenti potranno quindi importarlo ed utilizzare quella funzione, oggetto o classe di cui hanno bisogno, senza dover estendere tale modulo.

content/docs/nav.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
- id: lifting-state-up
3333
title: Lifting State Up
3434
- id: composition-vs-inheritance
35-
title: Composition vs Inheritance
35+
title: Composizione vs Ereditarità
3636
- id: thinking-in-react
3737
title: Thinking In React
3838
- title: Advanced Guides
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.BordoFigo {
2+
padding: 10px 10px;
3+
border: 10px solid;
4+
}
5+
6+
.BordoFigo-blue {
7+
border-color: blue;
8+
}
9+
10+
.Finestra-titolo {
11+
margin: 0;
12+
font-family: sans-serif;
13+
}
14+
15+
.Finestra-messaggio {
16+
font-size: larger;
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './1.css';
4+
5+
function BordoFigo(props) {
6+
return (
7+
<div className={'BordoFigo BordoFigo-' + props.colore}>
8+
{props.children}
9+
</div>
10+
);
11+
}
12+
13+
function FinestraBenvenuto() {
14+
return (
15+
<BordoFigo colore="blue">
16+
<h1 className="Finestra-titolo">Benvenuto/a!</h1>
17+
<p className="Finestra-messaggio">
18+
Ti ringraziamo per questa tua visita nella nostra
19+
nave spaziale!
20+
</p>
21+
</BordoFigo>
22+
);
23+
}
24+
25+
ReactDOM.render(
26+
<FinestraBenvenuto />,
27+
document.getElementById('root')
28+
);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
html, body, #root {
2+
width: 100%;
3+
height: 100%;
4+
}
5+
6+
.Pannello {
7+
width: 100%;
8+
height: 100%;
9+
}
10+
11+
.Pannello-sinistra {
12+
float: left;
13+
width: 30%;
14+
height: 100%;
15+
}
16+
17+
.Pannello-destra {
18+
float: left;
19+
width: 70%;
20+
height: 100%;
21+
}
22+
23+
.Contatti {
24+
width: 100%;
25+
height: 100%;
26+
background: lightblue;
27+
}
28+
29+
.Chat {
30+
width: 100%;
31+
height: 100%;
32+
background: pink;
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './2.css';
4+
5+
function Contatti() {
6+
return <div className="Contatti" />;
7+
}
8+
9+
function Chat() {
10+
return <div className="Chat" />;
11+
}
12+
13+
function Pannello(props) {
14+
return (
15+
<div className="Pannello">
16+
<div className="Pannello-sinistra">
17+
{props.sinistra}
18+
</div>
19+
<div className="Pannello-destra">{props.destra}</div>
20+
</div>
21+
);
22+
}
23+
24+
function App() {
25+
return (
26+
<Pannello sinistra={<Contatti />} destra={<Chat />} />
27+
);
28+
}
29+
30+
ReactDOM.render(<App />, document.getElementById('root'));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.BordoFigo {
2+
padding: 10px 10px;
3+
border: 10px solid;
4+
}
5+
6+
.BordoFigo-blue {
7+
border-color: blue;
8+
}
9+
10+
.Finestra-titolo {
11+
margin: 0;
12+
font-family: sans-serif;
13+
}
14+
15+
.Finestra-messaggio {
16+
font-size: larger;
17+
}

0 commit comments

Comments
 (0)