-
Is this possible? Can I access things in a store in middle of some business logic? I haven't found any getStore methods or something similar. Only hooks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hey @janniksam ! Yes, this is possible. When you create your store instance (via To access the state from the store instance, you have to call I've set up this sandbox to show a working example:
Hope that helps 👍 |
Beta Was this translation helpful? Give feedback.
-
Hey @jmyrland , thank you for the very quick response. 👍 :) I am very new to the web world (and JS / TS in general), so I have a follow up (unrelated) question. In the store.ts file I see a: const store = createStore<TodosModel>(todosStore); My reallife scenario looks a bit different, but it's pretty similar in general export const MyStore = createStore<IContext>({
accountInfo: accountInfo,
myFees: fees,
myOrders: orders,
myTokens: myTokens,
availablePools: availablePools,
recentOrders: recentOrders,
// thunks
updateAccountRelated: thunk((a) => {
a.myOrders.update();
a.myFees.update();
a.myTokens.update();
a.recentOrders.update();
}),
clearAccountRelated: thunk((a) => {
a.myOrders.clear();
a.myFees.clear();
a.myTokens.clear();
a.recentOrders.clear();
}),
}); The question, or lets say what I am confused about when it comes to Javascript: |
Beta Was this translation helpful? Give feedback.
-
Thank you. It works in my own code, everythings clear now👍 |
Beta Was this translation helpful? Give feedback.
Hey @janniksam !
Yes, this is possible. When you create your store instance (via
createStore()
) you can operate on this instance wherever you want to.To access the state from the store instance, you have to call
getState()
and for actionsgetActions()
.I've set up this sandbox to show a working example:
store/index.ts
business.ts
imports the store and exports a method to do some work.main.js
calls upon the method frombusiness.ts
. This should log the current state & add a todo to the list.Hope that helps 👍