我正在将lodash/fp与react一起使用,我是react新手。这个
documentation for _.get()
表示可以传递默认值:
如果解析值未定义,则使用defaultValue替换它。
但是,在签名不显示defaultValue并且将其作为第三个参数传递不起作用的情况下,它将被忽略:
import _ from "lodash/fp"
console.log("first:", _.get("email", profile.profile, "test"))
console.log("second:", _.get("email", profile.profile) ? _.get("email", profile.profile) : "test")
console.log("third:", profile.profile.email)
我进入控制台:
app.js:251 first: undefined
app.js:252 second: test
app.js:253 Uncaught TypeError: Cannot read property 'email' of undefined
既然它是未定义的,正如“第三个”日志中的错误所示,为什么第一个不给我空字符串而不是未定义的?
看见
related discussion
。
如何调用lodash fp并使用默认值?这就是getOr的目的吗?我尝试了getOr,但没有得到默认值。