DioxusLabs / DioxusLabs/dioxus

feat(store): support lens behavior extension

Open
#4,739 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

## Feature Request

Stores are a great addition
Among other perks they bring fine-grained reactivity to collections.

But DX could be further improved.
For example, right now we still regularly need to pass down store + index props to child components (.e.g when tracking selection or when the item may be removed from the collection).

It's what I've tried to achieve with https://github.com/gpoblon/dx-collection.
But to do so I have to implement wrappers which ends up looking like an anti-pattern because StoreItem wrappers implies to implement read/write/iter abstractions that return the `StoreItem` instead of the Lens. Which I'd like to avoid.

## Implement Suggestion

1. Expose a Lens::parent method.
To limit leaking the parent signal, the parent method could be a closure such as `pub fn with_parent(|store: Store, index: Index|

2. Make sure the types we need to impl custom behavior to lens are properly exposed (or provide a type alias).

Related request: impl a public `Lens::key()` or `Lens::index()` method.

## Ideal Result

Here's what I'd love to be able to do as a dioxus user:

```rust
// dioxus now provides a with_parent(Fn(Index, Store)) - T method
// dioxus now exposes `pub type StoreItem = Lens/GetWrite/IndexWrite?<...>;`
impl StoreItem {
pub fn is_selected(&self) -> bool {
let mut is_selected = false;
// store.is_selected is implemented by user, so Index needs to be exposed/manipulable
self.with_parent(|index, store| is_selected = store.is_selected(index))
is_selected
}
pub fn select(&self) { self.with_parent(|index, store| store.select(index)) } // store.select is implemented by user
pub fn remove(&self) { self.with_parent(|index, store| store.remove(index)) }
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.