Getter with parameters … #42
-
I really like Harlem, thanks for all your hard work. I may be missing a fundamental design contract of the store pattern, but I’m keen to have a getter that can take one (or more) parameters. My use case is as follows: I’m loading company brand information from an external data source. The brand information is immutable once loaded. The brand information includes a map of contact information, for example, departmental email addresses. I want to have a getter that takes a department identifier and then returns the corresponding departmental email address. That is, the email addresses are stored in a map within the store, and retrieved by means of the key as a parameter applied through a getter. Am I misunderstanding the store pattern in wanting this feature? Cheers, S t u a r t . |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi Stuart Thank you for your kind words - I really appreciate it. Although historically Vuex has enabled the use of getters with parameters, I have made the conscious decision to not include that functionality as part of Harlem as the same result can now be achieved using just a function and leaning on Vue's reactivity system to re-evaluate the function once a dependency changes. Given your use-case above, here's how you might achieve what you are describing: /*
Assuming you have a state structure something like:
{
departments: [
{
id,
email
}
]
}
*/
import {
state
} from './store';
export function getDepartmentEmail(departmentId: string): string | undefined {
return state.departments.find(({ id }) => id === departmentId)?.email;
} If you use the Hopefully that helps. Let me know if you have any further questions. Thanks |
Beta Was this translation helpful? Give feedback.
Hi Stuart
Thank you for your kind words - I really appreciate it.
Although historically Vuex has enabled the use of getters with parameters, I have made the conscious decision to not include that functionality as part of Harlem as the same result can now be achieved using just a function and leaning on Vue's reactivity system to re-evaluate the function once a dependency changes.
Given your use-case above, here's how you might achieve what you are describing: