Skip to content

GregoryEsberci/get-attribute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get-attribute

A small library for a simple problem.

get-attribute allows you to retrieve a value at a specified path within an object or array (similar to lodash get). But why use this library if lodash already exists? The reason is that this library returns a function that remains unchanged between calls, helping to prevent unnecessary React re-renders by memorizing the function and returning the same function for the same path. It has been created with React and React Native projects in mind but works in other JavaScript environments.

Example: View the example on CodeSandbox

Installation

Using npm:

npm install get-attribute

Using yarn:

yarn add get-attribute

Examples

const user = {
  address: {
    postCode: 'SW1P 3PA',
    street: ['20 Deans Yd'],
  },
};

const getUserPostCode = getAttribute('address.postCode');
const getUserStreet = getAttribute('address.street[0]');

getUserPostCode(user); // Result: "SW1P 3PA"
getUserStreet(user); // Result: "20 Deans Yd"
const users = [
  { id: 1, name: 'Alfred' },
  { id: 2, name: 'Bob' },
];

<FlatList data={users} keyExtractor={getAttribute('id')} /* other necessary props */ />

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published