Ramda.js R.view() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.view() answers a read question: what value sits at the piece a lens focuses? You pass a lens, then the data. The lens getter runs. You get that focused leftover, not a copy of the whole object. Native JavaScript’s cousin is obj.x or R.prop('x', obj). That is one key. view is data-last, so R.view(xLens) is already a function of one object — ready for R.map or R.pipe.

The official sample is the whole idea. R.lensProp('x') focuses the x key. Viewing {x: 1, y: 2} yields 1. Viewing {x: 4, y: 2} yields 4. Need to write a literal? That is R.set. Need a function of the current value? That is R.over. Need one top-level key with no lens? That is R.prop. Need a nested path with no lens? That is R.path. Previous Object helper is R.valuesIn. Next is R.where.

Syntax

Call R.view. Start with this line.

Ramda
R.view(lens, structure)
CallWhat it does
R.view(lens, structure)Primary signature.
R.view(...)Call the object helper.

Prefer import { view } from "ramda" when you want a named import.

Library declarations
Ramda
R.view(lens, structure)

Parameters

ParameterDescription
lensRequired. Lens. A focus built with R.lens, R.lensProp, R.lensIndex, or R.lensPath.
structureRequired. Any. The source object, array, or nested tree. Docs call this x. It is not mutated.

Return Value

Return ValueDescription
Focused leftoverWhatever the lens getter returns. A missing key is usually undefined. The source structure is not copied.

Ramda Examples

📚 Getting Started

The official Ramda object sample, then the same lens compared with set and over.

Example 1 — Official xLens

Focus x. Read 1, then read 4. y is never returned.

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

R.view(xLens, {x: 1, y: 2});
//=> 1

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

How It Works

R.lensProp('x') is R.lens(R.prop('x'), R.assoc('x')). view only uses the getter. That is the docs sample from ramdajs.com.

Example 2 — vs set, over, and prop

Same xLens. view reads. set writes a literal. over maps a function. prop matches view for one key.

Ramda
const xLens = R.lensProp('x');
const point = {x: 1, y: 2};

R.view(xLens, point);
//=> 1

R.set(xLens, 4, point);
//=> {x: 4, y: 2}

R.over(xLens, R.negate, point);
//=> {x: -1, y: 2}

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

How It Works

view never copies the object. set and over do. prop agrees here because the key is top-level. Prefer prop when you will never reuse a lens. Prefer view when the same focus must also support set and over, or when the slot is an index or a nested path.

📈 Practical Patterns

An index, a nested path, a missing key, then a curried helper mapped over rows.

Example 3 — lensIndex

Read the first array slot. The rest of the list is never returned.

Ramda
R.view(R.lensIndex(0), ['foo', 'bar', 'baz']);
//=> 'foo'

R.view(R.lensIndex(2), ['foo', 'bar', 'baz']);
//=> 'baz'
Try It Yourself

How It Works

R.lensIndex(0) is the same focus R.over uses in its official head sample. view stops after the getter. over would write a transformed item back into a new array.

Example 4 — Nested lensPath and a Missing Key

Read a nested name. A missing key is undefined. The original tree is not copied.

Ramda
const nameLens = R.lensPath(['user', 'name']);
const orig = { user: { name: 'ada', city: 'Paris' } };

R.view(nameLens, orig);
//=> 'ada'

R.view(R.lensProp('x'), {});
//=> undefined
Try It Yourself

How It Works

R.lensPath walks the trail. R.lensProp('name') would miss, because name is not top-level. R.path is the no-lens cousin. Labs that print with JSON.stringify turn undefined into null — these outputs use String() for the hole.

Example 5 — Curry and Map

Fill the lens to pluck one field from many rows. The leftover is a function of one object.

Ramda
const getX = R.view(R.lensProp('x'));

R.map(getX, [
  { x: 1, y: 2 },
  { x: 4, y: 2 }
]);
//=> [1, 4]
Try It Yourself

How It Works

Because view is arity 2, filling the lens leaves a function of one record. Native row.x would need an arrow wrapper on every row. Need to write instead? Switch to R.set or R.over.

Browser Support

R.view() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.16.0+ · Any JS runtime

Ramda.js R.view()

Load Ramda 0.16.0 or later via CDN or npm, then call R.view with a lens and a structure. No extra browser API is required.

100% With Ramda 0.16.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.view() Universal

Bottom line: Safe in any project that includes Ramda 0.16.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.view(lens, structure) reads the value focused by lens. You get that focused leftover, not a copy of the whole structure. Added in v0.16.0. The original is not mutated.
const xLens = R.lensProp('x'). R.view(xLens, {x: 1, y: 2}) is 1. R.view(xLens, {x: 4, y: 2}) is 4.
view only reads the focus. set replaces the focus with a value you already know. over reads the focus, runs a function, and writes the result. view does not copy the structure.
prop reads one top-level key. path reads a nested trail. view uses any lens: lensProp, lensIndex, lensPath, or a custom R.lens. For one key, view(lensProp('x'), obj) matches prop('x', obj).
view returns whatever the getter returns, often undefined. R.view(R.lensProp('x'), {}) is undefined. JSON.stringify turns that hole into null in labs; use String() when you need the real hole.
No. R.view is a stable Ramda object helper from v0.16.0. It is not deprecated, experimental, or non-standard.

Continue to R.where()

Learn how to test an object against a spec of predicates using Ramda R.where().

R.where() 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