Answer the question
In order to leave comments, you need to log in
How to replace the .map() function in my case?
Hello, the problem is this:
I am using react-csv in a React application:
...
render() {
return (
...
{rows.map(row => (
<CSVLink
key={row.number}
className="btn btn-secondary btn-download"
data={rows}
separator={";"}
filename="netpositions.csv"
>
<span className="oi oi-data-transfer-download" />
</CSVLink>
))}
...
)
}
Answer the question
In order to leave comments, you need to log in
The question was answered by the author of the question, I, in turn, will try to explain what happened.
Rendering in react always follows the least laborious path, namely, it calculates the delta (difference) between the new state and the previous one, if any. For example:
class TodoList extends React.Component {
state = {
todos: [
'Commit',
'Push'
]
}
render() {
return <ul>
{this.state.todos.map(item => {
return <li>{ todo }</li>
}
</ul>
}
}
const todos = [
'Init',
'Commit',
'Push'
]
// Начальный стейт
<ul>
<li>Commit</li>
<li>Push</li>
</ul>
// Добавлен элемент
<ul>
<li>Init</li> // <- разница начинается здесь и до конца древа
<li>Commit</li>
<li>Push</li>
</ul>
const todos = [
'Commit',
'Push',
'Merge'
]
// Начальный стейт
<ul>
<li>Commit</li>
<li>Push</li>
</ul>
// Добавлен элемент
<ul>
<li>Commit</li>
<li>Push</li>
<li>Merge</li> <- разница начинается здесь, от начала и до сих по ничего не менялось
</ul>
<li>Commit</li>
they <li>Push</li>
did not change, but react is not smart enough to understand this. To help him, you should use a special prop key={}
. It can be a value of any type, the only requirement is that the value must stably identify the corresponding data. class TodoList extends React.Component {
state = {
todos: [
{ id: 0, text: 'Commit' },
{ id: 1, text: 'Push' }
]
}
render() {
return <ul>
{this.state.todos.map(item => {
return <li key={todo.id}>{ todo.text }</li>
}
</ul>
}
}
const todos = [
{ id: 2, text: 'Init' },
{ id: 0, text: 'Commit' },
{ id: 1, text: 'Push' }
]
// Начальный стейт
<ul>
<li>Commit</li> // id 0
<li>Push</li> // id 1
</ul>
// Добавлен элемент
<ul>
<li>Init</li> // id 2 новый элемент отобразится в начале
<li>Commit</li> // id 0
<li>Push</li> // id 1
</ul>
Math.random()
as a key, as you are almost guaranteed to always get unstable ids. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question