admin管理员组

文章数量:1289634

I am still rather new to building React/Redux apps. I have been building applications and looking at the code of other applications to learn best practice.

Looking over someone's Reducer, many of the case statements looked like so:

case RECEIVE_CURRENT_TEAM:
  return _.merge({}, state, {current_team: action.team})

I wanted to know what _. before merge is doing.

The way I would write the above in my reducer is as follows:

case RECEIVE_CURRENT_TEAM:
  return merge({}, state, {current_team: action.team})

I wanted to know the benefit if there was any to _. as I am at the very beginning of building out my next app.

I found some info, but it was vague and linked documentation for _ extend in JavaScript, but the link was broken.

Thanks for the help!

I am still rather new to building React/Redux apps. I have been building applications and looking at the code of other applications to learn best practice.

Looking over someone's Reducer, many of the case statements looked like so:

case RECEIVE_CURRENT_TEAM:
  return _.merge({}, state, {current_team: action.team})

I wanted to know what _. before merge is doing.

The way I would write the above in my reducer is as follows:

case RECEIVE_CURRENT_TEAM:
  return merge({}, state, {current_team: action.team})

I wanted to know the benefit if there was any to _. as I am at the very beginning of building out my next app.

I found some info, but it was vague and linked documentation for _ extend in JavaScript, but the link was broken.

http://documentcloud.github.io/underscore/#extend

Thanks for the help!

Share Improve this question asked Oct 16, 2017 at 1:04 hanchohancho 1,4373 gold badges23 silver badges44 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

_ is not a Javascript feature. It is a monly-used library.

It could either be a reference to lodash or underscore.

Both libraries have great documentation so you can lookup what each function does.

The other folks have mentioned that _ is usually either lodash or underscore, but you should know there's nothing mystical or magical about these functions/libraries. You can make one too.

var _ = (function() {
	
  function greet() {
  	console.log("hi there!");
  }
  
  return {
  	greet
  }
  
})()

_.greet();

If you don't want lodash/underscore (which exactly is _), you may try for example ES7 syntax

Object.assign({}, state, {current_team: action.team})

or

{ ...state, current_team: action.team }

本文标签: reactjsWhat does quotquot (insert function) do in JavaScript (React example)Stack Overflow