Ramda’s R.lensProp() answers a property question: how do I get and set one key without rewriting the rest of the object every time? You pass a property name. You get a lens. Then R.view reads that key, R.set replaces it, and R.over maps a function over it. The original object is not mutated.
The official sample is the whole idea. R.lensProp('x') focuses the x field of {x: 1, y: 2}. View returns 1. Set returns a new point with x: 4. Over with R.negate returns x: -1. Need a nested trail? That is the previous helper, R.lensPath. Need a custom getter and setter? That is R.lens. Need a one-off write with no view? That is R.assoc.
Syntax
Call R.lensProp. Start with this line.
Ramda
R.lensProp(k)
Call
What it does
R.lensProp(k)
Primary signature.
R.lensProp(...)
Call the object helper.
Prefer import { lensProp } from "ramda" when you want a named import.
Library declarations
Ramda
R.lensProp(k)
Parameters
Parameter
Description
k
Required. String. The property name to focus. Official: 'x'.
Return Value
Return Value
Description
Lens
A focus you pass to R.view, R.set, and R.over. You rarely call the lens function yourself.
Ramda Examples
📚 Getting Started
The official x lens with view, set, and over, then the same focus written as R.lens.
R.over(nameLens, R.toUpper) waits for the object. R.view(nameLens) waits for the object. Drop them into R.pipe when you need a longer chain.
Compatibility
Browser Support
R.lensProp() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.14.0+ · Any JS runtime
Ramda.js R.lensProp()
Load Ramda 0.14.0 or later via CDN or npm, then call R.lensProp with a property name. No extra browser API is required.
100%With Ramda 0.14.0+
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
R.lensProp()Universal
Bottom line: Safe in any project that includes Ramda 0.14.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].
Frequently Asked Questions
R.lensProp(k) returns a lens whose focus is property k. R.view reads that key. R.set replaces it. R.over maps a function over it. Added in v0.14.0. The original object is not mutated.
lensProp(k) is R.lens(R.prop(k), R.assoc(k)). lens is the general constructor. lensPath walks a nested trail such as ['user', 'name']. A one-key path matches lensProp; nested keys need lensPath.
R.view returns undefined, the same as R.prop. R.set adds the key on a clone with R.assoc, so {} can become {name: 'Ada'}. The original object is not mutated.
No. R.lensProp('name') only looks at a top-level name. R.view(R.lensProp('name'), {user: {name: 'Ada'}}) is undefined. Use R.lensPath(['user', 'name']) for a nested trail.
No. R.lensProp is a stable Ramda object helper from v0.14.0. It is not deprecated, experimental, or non-standard.