Ramda.js R.lensProp() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

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)
CallWhat 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

ParameterDescription
kRequired. String. The property name to focus. Official: 'x'.

Return Value

Return ValueDescription
LensA 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.

Example 1 — Official x Lens

Read x, replace x, map x with negate. y stays 2.

Ramda
const xLens = R.lensProp('x');

R.view(xLens, {x: 1, y: 2});
//=> 1
R.set(xLens, 4, {x: 1, y: 2});
//=> {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2});
//=> {x: -1, y: 2}
Try It Yourself

How It Works

'x' is the focused key. That is the docs sample from ramdajs.com.

Example 2 — vs R.lens

Both views return the same x.

Ramda
const point = {x: 1, y: 2};

R.view(R.lensProp('x'), point);
//=> 1
R.view(R.lens(R.prop('x'), R.assoc('x')), point);
//=> 1
Try It Yourself

How It Works

R.lens is the constructor. lensProp('x') is that pair already wired. Keep lens when the getter or setter is not prop plus assoc.

📈 Practical Patterns

A one-key path vs a nested miss, a missing key that set adds, then a curried over on name.

Example 3 — vs R.lensPath

A one-key path matches. A nested name is invisible to lensProp.

Ramda
const user = {name: 'Ada', city: 'London'};
const nested = {user: {name: 'Ada'}};

R.view(R.lensProp('name'), user);
//=> 'Ada'
R.view(R.lensPath(['name']), user);
//=> 'Ada'
R.view(R.lensProp('name'), nested);
//=> undefined
Try It Yourself

How It Works

nested has no own name. The name lives under user. That trail is R.lensPath(['user', 'name']).

Example 4 — Missing Key

view is undefined. set adds the key on a clone. The original stays empty.

Ramda
const nameLens = R.lensProp('name');
const empty = {};

R.view(nameLens, empty);
//=> undefined
R.set(nameLens, 'Ada', empty);
//=> {name: 'Ada'}
empty;
//=> {}
Try It Yourself

How It Works

That is assoc adding a key. The source object is never assigned to. Guard with R.has('name', obj) if you do not want new keys.

Example 5 — over with Curry

Uppercase the name, then read it. Both helpers are data-last.

Ramda
const nameLens = R.lensProp('name');
const shoutName = R.over(nameLens, R.toUpper);
const readName = R.view(nameLens);
const user = {name: 'Ada', city: 'London'};

shoutName(user);
//=> {name: 'ADA', city: 'London'}
readName(shoutName(user));
//=> 'ADA'
Try It Yourself

How It Works

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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.
const xLens = R.lensProp('x'). R.view(xLens, {x: 1, y: 2}) is 1. R.set(xLens, 4, {x: 1, y: 2}) is {x: 4, y: 2}. R.over(xLens, R.negate, {x: 1, y: 2}) is {x: -1, y: 2}.
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.

Continue to R.mapKeys()

Learn how to rewrite every own key using Ramda R.mapKeys().

R.mapKeys() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful