-
-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selector own props #118
Comments
const Component = createComponent(
props => $data.map(list => list[props.id]),
(props, state) => {
return <div />
}
) |
Thanky you @goodmind for the answer, this will do it. But can you do this using hooks. I connect my stores using |
@ghalex You can use const OwnGate = createGate();
const DataSlice = combine(
SomeStoreWithData,
OwnGate.state,
(data, props) => selectSliceFromData(state, props)
)
const SomeComponent = (props) => {
useGate(OwnGate, props);
return <InnerComponent />
}
const InnerComponent = () => {
const dataSlice = useStore(DataSlice);
return <div>{dataSlice}</div>
} May be it should work in one component too. |
Hi @Laiff, thx for the replay but I think I found the solution myself, I will posted here if someone else is searching for something like this: const Component = ({ id }: Props) => {
const user = useStore(store.$currentUser);
const company = useStore(store.$all.map(all => all.find(c => c.id === id)));
// here I have the company selected from the $all store, and it works if $all store changes
} and this is using hook |
@ghalex Nice solution, but does it recalculate stored value on changing props? |
Yes, it is |
we have a solution for this issue, will be published in next release of effector-react https://github.com/zerobias/effector/blob/master/src/react/useStore.js#L26 const Component = ({id}) => {
const user = useStoreMap({
store: users,
keys: [id],
fn: (users, [id]) => users[id],
})
}
const users = createStore({
alex: {age: 20, name: "Alex"},
john: {age: 30, name: "John"},
}) It can be used for your own hooks const readUserAge = id => useStoreMap({
store: users,
keys: [id],
fn: (users, [id]) => users[id].age,
})
const UserAge = ({id}) => {
const age = readUserAge(id)
} In this example |
Also maybe it makes sense to add alternative syntax, similar with official hooks. const Component = ({id}) => {
const user = useStoreMap(users$, (users, [id]) => {
return users[id]
}, [id])
}
const users$ = createStore({
alex: {age: 20, name: "Alex"},
john: {age: 30, name: "John"},
}) |
Method |
I know you can use $store.map to select part of a store but what if I need to select a part of a store using a parameter. Example:
if I have and
cont id = "alex"
in a component somewhere is there a use to select only alex data and get the update if that is changing ?Using reselect I could do something like:
The text was updated successfully, but these errors were encountered: