import * as React from "react";
import { createStore, createEmitter, createActions } from "fluxgate";
import { useState, StoreProvider } from "fluxgate-hooks";
interface User {
id: string;
login: string;
}
interface UserStore {
user: User;
loading: boolean;
}
const Stores = {
user = "user",
};
const userEmitter = createEmitter();
const userStore = createStore<UserStore>(userEmitter, {
user: null,
loading: false,
}, "user");
function loadUser(id: string) {
userStore.setState({ loading: true });
readUserFromApi(id).then((user) => { // readUserFromApi - is not defined in the example
userStore.setState({
user,
loading: false,
});
});
}
const userActions = createActions(userStore, {
loadUser,
});
function AppContainer() {
const userState = useStore<UserStore>(Stores.user);
if (userState.user) {
return (
<StoreProvider
stores={{
[Stores.user]: userStore,
}}
actions={{
[Stores.user]: userActions,
}}
>
<div>
{user.id} {user.login}
</div>
</StoreProvider>
);
}
return (
<button onClick={userActions.loadUser}>Load User</button>
);
}
ReactDom.render(<AppContainer />, window.document.querySelector('.app'));
-
Notifications
You must be signed in to change notification settings - Fork 0
License
codetraceio/fluxgate-hooks
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published