-3

I'm a newbie I've download some source in this link https://github.com/the-road-to-learn-react/react-redux-example i have some proplem in file src/app.js line 4

const applyFilter = searchTerm => article =>
  article.title.toLowerCase().includes(searchTerm.toLowerCase());

why this can't work with

const applyFilter = searchTerm => article =>{
  article.title.toLowerCase().includes(searchTerm.toLowerCase());
} 

or

const applyFilter = searchTerm => {article =>
  article.title.toLowerCase().includes(searchTerm.toLowerCase());
}

and in line 14 when call the funtion

articles.filter(applyFilter(searchTerm))
const applyFilter = searchTerm => article =>
  article.title.toLowerCase().includes(searchTerm.toLowerCase());

this in my mind is call an arrow function inside an arrow function? How can they put var 'article' in??

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Khoa Trumcuoi
  • 33
  • 1
  • 5

1 Answers1

0

Arrow function syntax is kinda like this

(a) => b(a);

is equivalent to

function(a) {
    return b(a);
}

However, when you add {} to an arrow function, it changes the meaning:

(a) => { b(a); }

is equivalent to

function(a) {
    b(a); 
    // notice that this doesn't actually return anything, 
    // it just calls a function and does nothing with the result
}

So you have to add a return statement when you add brackets

(a) => { return b(a); }
TKoL
  • 13,158
  • 3
  • 39
  • 73