为什么 react-redux connect() 会给出这样的默认值?
Why react-redux connect() give a default value like this?
我只是想知道为什么在初始化下面给出的函数的参数后给出一个“={}”,这是一个空的默认值,有人可以回答这个问题吗?
return function connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
{
pure = true,
areStatesEqual = strictEqual,
areOwnPropsEqual = shallowEqual,
areStatePropsEqual = shallowEqual,
areMergedPropsEqual = shallowEqual,
...extraOptions
} = {} ){ // code is here }
您可以为对象的参数和解构的属性设置默认值。 ={}
是您要解构的函数参数的默认值。如果您不定义此默认值,并且该方法的调用者不提供对象,则调用将失败并出现错误:
const demo = ({ a = 1, b = 2 }) => ({ a, b });
console.log(demo({})); // works fine because it tries to destructure an object
console.log(demo()); // fails because the destructuring target is undefined
如果您设置默认值 ={}
它也可以处理未定义的值:
const demo = ({ a = 1, b = 2 } = {}) => ({ a, b });
console.log(demo()); // works because it has a default value
我只是想知道为什么在初始化下面给出的函数的参数后给出一个“={}”,这是一个空的默认值,有人可以回答这个问题吗?
return function connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
{
pure = true,
areStatesEqual = strictEqual,
areOwnPropsEqual = shallowEqual,
areStatePropsEqual = shallowEqual,
areMergedPropsEqual = shallowEqual,
...extraOptions
} = {} ){ // code is here }
您可以为对象的参数和解构的属性设置默认值。 ={}
是您要解构的函数参数的默认值。如果您不定义此默认值,并且该方法的调用者不提供对象,则调用将失败并出现错误:
const demo = ({ a = 1, b = 2 }) => ({ a, b });
console.log(demo({})); // works fine because it tries to destructure an object
console.log(demo()); // fails because the destructuring target is undefined
如果您设置默认值 ={}
它也可以处理未定义的值:
const demo = ({ a = 1, b = 2 } = {}) => ({ a, b });
console.log(demo()); // works because it has a default value