* chore: rename APIs and Microservices to include "Backend" (#42515) * fix typo * fix typo * undo change * Corrected grammar mistake Corrected a grammar mistake by removing a comma. * change APIs and Microservices cert title * update title * Change APIs and Microservices certi title * Update translations.json * update title * feat(curriculum): rename apis and microservices cert * rename folder structure * rename certificate * rename learn Markdown * apis-and-microservices -> back-end-development-and-apis * update backend meta * update i18n langs and cypress test Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: add development to front-end libraries (#42512) * fix: added-the-word-Development-to-front-end-libraries * fix/added-the-word-Development-to-front-end-libraries * fix/added-word-development-to-front-end-libraries-in-other-related-files * fix/added-the-word-Development-to-front-end-and-all-related-files * fix/removed-typos-from-last-commit-in-index.md * fix/reverted-changes-that-i-made-to-dependecies * fix/removed xvfg * fix/reverted changes that i made to package.json * remove unwanted changes * front-end-development-libraries changes * rename backend certSlug and README * update i18n folder names and keys * test: add legacy path redirect tests This uses serve.json from the client-config repo, since we currently use that in production * fix: create public dir before moving serve.json * fix: add missing script * refactor: collect redirect tests * test: convert to cy.location for stricter tests * rename certificate folder to 00-certificates * change crowdin config to recognise new certificates location * allow translations to be used Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com> * add forwards slashes to path redirects * fix cypress path tests again * plese cypress * fix: test different challenge Okay so I literally have no idea why this one particular challenge fails in Cypress Firefox ONLY. Tom and I paired and spun a full build instance and confirmed in Firefox the page loads and redirects as expected. Changing to another bootstrap challenge passes Cypress firefox locally. Absolutely boggled by this. AAAAAAAAAAAAAAA * fix: separate the test Okay apparently the test does not work unless we separate it into a different `it` statement. >:( >:( >:( >:( Co-authored-by: Sujal Gupta <55016909+heysujal@users.noreply.github.com> Co-authored-by: Noor Fakhry <65724923+NoorFakhry@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
266 lines
7.8 KiB
Markdown
266 lines
7.8 KiB
Markdown
---
|
|
id: 5a24c314108439a4d403618a
|
|
title: Usar array.map() para renderizar elementos dinamicamente
|
|
challengeType: 6
|
|
forumTopicId: 301417
|
|
dashedName: use-array-map-to-dynamically-render-elements
|
|
---
|
|
|
|
# --description--
|
|
|
|
Renderização condicional é útil, mas você pode precisar de seus componentes para renderizar um número desconhecido de elementos. Muitas vezes em programação reativa, um programador não tem como saber o estado de uma aplicação até o tempo de execução, porque muito depende da interação do usuário com esse programa. Os programadores precisam escrever o seu código para lidar corretamente com esse estado desconhecido antes do tempo. Usar `Array.map()` em React ilustra este conceito.
|
|
|
|
Por exemplo, você cria um aplicativo "Lista de Coisas a Fazer" simples. Como programador, você não tem como saber quantos itens um usuário pode ter em sua lista. Você precisa configurar seu componente para renderizar dinamicamente o número correto de elementos da lista muito antes de alguém usar o programa decidir que hoje é o dia da lavagem.
|
|
|
|
# --instructions--
|
|
|
|
O editor de código tem a maioria do componente `MyToDoList` configurado. Parte deste código deve parecer familiar se você completou o desafio de formulário controlado. Você notará um `textarea` e um `button`, junto com alguns métodos que rastreiam seus estados, mas nada é renderizado na página ainda.
|
|
|
|
Dentro do `constructor`, crie um objeto `this.state` e defina dois estados: `userInput` deve ser inicializado como uma string vazia, e `toDoList` deve ser inicializado como um array vazio. Em seguida, exclua o comentário no método `render()` ao lado da variável `items`. Em seu lugar, mapeie sobre o array `toDoList` armazenado no estado interno do componente e renderize dinamicamente um `li` para cada item. Tente inserir a string `eat, code, sleep, repeat` dentro do `textarea`, depois clique no botão e veja o que acontece.
|
|
|
|
**Observação:** você pode saber que todos os elementos filhos do irmão criados por uma operação de mapeamento como esta precisam ser fornecidos com um atributo `key` exclusivo. Não se preocupe, este é o tema do próximo desafio.
|
|
|
|
# --hints--
|
|
|
|
O componente MyToDoList deve existir e renderizar à página.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
return mockedComponent.find('MyToDoList').length === 1;
|
|
})()
|
|
);
|
|
```
|
|
|
|
O primeiro filho do `MyToDoList` deve ser um elemento `textarea`.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
return (
|
|
mockedComponent.find('MyToDoList').children().childAt(0).type() ===
|
|
'textarea'
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
O segundo filho do `MyToDoList` deve ser um elemento `br`.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
return (
|
|
mockedComponent.find('MyToDoList').children().childAt(1).type() === 'br'
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
O terceiro filho do `MyToDoList` deve ser um elemento `button`.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
return (
|
|
mockedComponent.find('MyToDoList').children().childAt(2).type() ===
|
|
'button'
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
O estado de `MyToDoList` deve ser inicializado com `toDoList` como um array vazio.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
const initialState = mockedComponent.state();
|
|
return (
|
|
Array.isArray(initialState.toDoList) === true &&
|
|
initialState.toDoList.length === 0
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
O estado de `MyToDoList` deve ser inicializado com `userInput` como uma string vazia.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
const initialState = mockedComponent.state();
|
|
return (
|
|
typeof initialState.userInput === 'string' &&
|
|
initialState.userInput.length === 0
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
Quando o botão `Create List` é clicado, o componente `MyToDoList` deve retornar, dinamicamente, uma lista não ordenada que contém um elemento de item de lista para cada item de uma lista separada por vírgulas inserida no elemento `textarea`.
|
|
|
|
```js
|
|
(() => {
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyToDoList));
|
|
const simulateChange = (el, value) =>
|
|
el.simulate('change', { target: { value } });
|
|
const state_1 = () => {
|
|
return mockedComponent.find('ul').find('li');
|
|
};
|
|
const setInput = () => {
|
|
return simulateChange(
|
|
mockedComponent.find('textarea'),
|
|
'testA, testB, testC'
|
|
);
|
|
};
|
|
const click = () => {
|
|
return mockedComponent.find('button').simulate('click');
|
|
};
|
|
const state_2 = () => {
|
|
const nodes = mockedComponent.find('ul').find('li');
|
|
return { nodes, text: nodes.reduce((t, n) => t + n.text().trim(), '') };
|
|
};
|
|
const setInput_2 = () => {
|
|
return simulateChange(
|
|
mockedComponent.find('textarea'),
|
|
't1, t2, t3, t4, t5, t6'
|
|
);
|
|
};
|
|
const click_1 = () => {
|
|
return mockedComponent.find('button').simulate('click');
|
|
};
|
|
const state_3 = () => {
|
|
const nodes = mockedComponent.find('ul').find('li');
|
|
return { nodes, text: nodes.reduce((t, n) => t + n.text().trim(), '') };
|
|
};
|
|
const awaited_state_1 = state_1();
|
|
const awaited_setInput = setInput();
|
|
const awaited_click = click();
|
|
const awaited_state_2 = state_2();
|
|
const awaited_setInput_2 = setInput_2();
|
|
const awaited_click_1 = click_1();
|
|
const awaited_state_3 = state_3();
|
|
assert(
|
|
awaited_state_1.length === 0 &&
|
|
awaited_state_2.nodes.length === 3 &&
|
|
awaited_state_3.nodes.length === 6 &&
|
|
awaited_state_2.text === 'testAtestBtestC' &&
|
|
awaited_state_3.text === 't1t2t3t4t5t6'
|
|
);
|
|
})();
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```jsx
|
|
ReactDOM.render(<MyToDoList />, document.getElementById('root'));
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```jsx
|
|
const textAreaStyles = {
|
|
width: 235,
|
|
margin: 5
|
|
};
|
|
|
|
class MyToDoList extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
// Change code below this line
|
|
|
|
// Change code above this line
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
this.handleChange = this.handleChange.bind(this);
|
|
}
|
|
handleSubmit() {
|
|
const itemsArray = this.state.userInput.split(',');
|
|
this.setState({
|
|
toDoList: itemsArray
|
|
});
|
|
}
|
|
handleChange(e) {
|
|
this.setState({
|
|
userInput: e.target.value
|
|
});
|
|
}
|
|
render() {
|
|
const items = null; // Change this line
|
|
return (
|
|
<div>
|
|
<textarea
|
|
onChange={this.handleChange}
|
|
value={this.state.userInput}
|
|
style={textAreaStyles}
|
|
placeholder='Separate Items With Commas'
|
|
/>
|
|
<br />
|
|
<button onClick={this.handleSubmit}>Create List</button>
|
|
<h1>My "To Do" List:</h1>
|
|
<ul>{items}</ul>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```jsx
|
|
const textAreaStyles = {
|
|
width: 235,
|
|
margin: 5
|
|
};
|
|
|
|
class MyToDoList extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = {
|
|
toDoList: [],
|
|
userInput: ''
|
|
};
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
this.handleChange = this.handleChange.bind(this);
|
|
}
|
|
handleSubmit() {
|
|
const itemsArray = this.state.userInput.split(',');
|
|
this.setState({
|
|
toDoList: itemsArray
|
|
});
|
|
}
|
|
handleChange(e) {
|
|
this.setState({
|
|
userInput: e.target.value
|
|
});
|
|
}
|
|
render() {
|
|
const items = this.state.toDoList.map((item, i) => {
|
|
return <li key={i}>{item}</li>;
|
|
});
|
|
return (
|
|
<div>
|
|
<textarea
|
|
onChange={this.handleChange}
|
|
value={this.state.userInput}
|
|
style={textAreaStyles}
|
|
placeholder='Separate Items With Commas'
|
|
/>
|
|
<br />
|
|
<button onClick={this.handleSubmit}>Create List</button>
|
|
<h1>My "To Do" List:</h1>
|
|
<ul>{items}</ul>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
```
|