![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
@penumbra-zone/getters
Advanced tools
Convenience getters for the deeply nested optionals of Penumbra's protobuf types
Getters were designed to solve a common pain point when working with deserialized Protobuf messages: accessing deeply nested, often optional properties.
For example, let's say you have an AddressView
, and you want to render an AddressIndex
. You'd need render it conditionally, like so:
<div>
{addressView.addressView.case === 'decoded' && addressView.addressView.value.index?.account && (
<span>{addressView.addressView.value.index.account}</span>
)}
</div>
Not very readable, and pretty annoying to type. But it can get even worse! Imagine you now have a MemoView
, which optionally contains an AddressView
. You still want to render an AddressIndex
. So you'd render it with even more conditions, like so:
<div>
{memoView.memoView.case === 'visible' &&
memoView.memoView.value.plaintext?.returnAddress.addressView.case === 'decoded' &&
memoView.memoView.value.plaintext.returnAddress.addressView.value.index?.account && (
<span>{addressView.addressView.value.index.account}</span>
)}
</div>
This quickly gets pretty cumbersome. You could, of course, throw a bunch of type guards at the top of your component to simplify your markup:
if (memoView.memoView.case !== 'visible') throw new Error()
if (memoView.memoView.value.plaintext?.returnAddress.addressView.case !== 'decoded') throw new Error()
if (typeof memoView.memoView.value.plaintext.returnAddress.addressView.value.index?.account === 'undefined') throw new Error()
<div>
<span>{addressView.addressView.value.index.account}</span>
</div>
This works, but you still have a bunch of boilerplate code crowding your component.
Getters solve all that. Getters are tiny, composable functions, inspired by the functional programming lens pattern, that allow you to get at deeply nested, often optional properties without all the boilerplate.
Let's solve the above problem with getters. First, let's create a getter that, when given a MemoView
, returns its AddressView
:
const getAddressView = createGetter((memoView?: MemoView) =>
memoView.memoView.case === 'visible' ? memoView.memoView.plaintext?.returnAddress : undefined,
);
getAddressView()
is now a simple function that can be called with a MemoView
, like this: getAddressView(memoView)
. It will then return the AddressView
, or throw if it's undefined
.
OK, next, let's create another getter that, when given an AddressView
, returns its AddressIndex
:
const getAddressIndex = createGetter((addressView?: AddressView) =>
addressView?.addressView.case === 'decoded' ? addressView.addressView.value.index : undefined,
);
Again, getAddressIndex()
is a simple function that can be called with an AddressView
, like this: getAddressIndex(addressView)
. It will then return the AddressIndex
, or throw if it's undefined
.
Since we defined these two functions with createGetter()
, though, they have a pipe
method that let us chain them together. That way, we can easily create a getter that, when given a MemoView
, will return an AddressIndex
:
const getAddressIndexFromMemoView = getAddressView.pipe(getAddressIndex);
OK, now we can quickly clean up our component code:
<div>
<span>{getAddressIndexFromMemoView(memoView)}</span>
</div>
Way better!
At this point, it's worth mentioning that getters are required by default. If any step along the getter chain above returns undefined
, they will throw a GetterMissingValueError
.
(It might seem unintuitive that getter functions are required, but are defined with an optional argument -- e.g., createGetter((addressView?: AddressView) => ... )
. Without that optionality, you'd get a TypeScript complaint like Type 'undefined' is not assignable to type 'AddressView'.
Getters assume that they can be passed undefined
; otherwise, TypeScript would make pipe()
ing impossible, since deeply nested properties are often optional. Don't worry, though: createGetter
ensures that your getters still throw if they get undefined
, which is what guarantees type safety.)
What if the value you're getting is optional, though? What if you don't want your getter to throw if either the value it's passed, or the value it returns, is undefined
? That's what the .optional()
property on the getter is for:
const addressView = getAddressView.optional(memoView)
<div>
{addressView && <AddressViewComponent addressView={addressView} />}
</div>
Or, if you want to chain multiple getters together and make the whole chain optional, call .optional()
on the first getter in the chain (which will then mark the rest of the chain as optional, too):
const getAddressIndexFromMemoView = getAddressView.optional().pipe(getAddressIndex);
const addressIndex = getAddressIndexFromMemoView(memoView)
<div>
{addressIndex && <AddressIndex addressIndex={addressIndex} />}
</div>
FAQs
Convenience getters for the deeply nested optionals of Penumbra's protobuf types
The npm package @penumbra-zone/getters receives a total of 0 weekly downloads. As such, @penumbra-zone/getters popularity was classified as not popular.
We found that @penumbra-zone/getters demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.