Javascript usage
Installation
The Killua package lives in npm (opens in a new tab). To install the latest stable version, run the following command:
# npm
npm install killua
# yarn
yarn add killua
Setup provider
with page directory
Go to pages/_app.jsx
(create it if it doesn't exist) and wrap the Component
with the SSRKilluaProvider
:
import { SSRKilluaProvider } from 'killua';
export default function App({ Component, pageProps }) {
return (
<SSRKilluaProvider>
<Component {...pageProps} />
</SSRKilluaProvider>
);
}
with app directory
- Go to your
app/providers.jsx
(create it if it doesn't exist) and wrap the Component with theSSRKilluaProvider
:
'use client';
import { SSRKilluaProvider } from 'killua';
export default function Providers({ children }) {
return <SSRKilluaProvider>{children}</SSRKilluaProvider>;
}
- Go to your root layout page and wrap it with the
Providers.jsx
component:
import Providers from './providers';
export default function RootLayout({ children }) {
return (
<html lang="en" className="dark">
<body>
<Providers>{children}</Providers>
</body>
</html>
);
}
Create thunder
Note that thunder
refers to a key within local storage.
Certainly, sensitive data should not be stored on local storage. The purpose of encrypting local storage in this method is to prevent regular users from having direct access to the data stored in local storage and also to prevent them from modifying it directly.
- Create a
thunders
directory for the thunder configuration. - Create the thunder configuration file, for example:
counter.js
. - Set up the configuration:
import { thunder } from 'killua';
const thunderCounter = thunder({
key: 'counter', // unique key for local storage, without starting with "thunder"
encrypt: false, // if true, the data will be encrypted before being stored in local storage
default: 1, // initial value for the thunder
expire: 1, // // null to disable the expiration timer, or a number indicating the expiration time in minutes
reducers: {
// optional
increment: (thunder) => thunder + 1,
incrementWithPayload: (thunder, payload) => thunder + payload,
reset: () => 1,
},
selectors: {
// optional
getCounterPlusOne: (thunder) => thunder + 1,
getCounterPlusPayload: (thunder, payload) => thunder + payload,
},
});
export { thunderCounter };
Use thunder
The 'thunder' and the return values of methods on the 'selectors' object may be undefined during the first render in SSR. If you want to use their return values, check if 'isReadyInSsr' is true before accessing them.
all desctructer property from thunder config is optional.
import { thunderCounter } from '../thunders/counter';
import { useKillua } from 'killua';
const Counter = () => {
const {
thunder: thunderCounterState,
setThunder: thunderCounterSetState,
reducers: thunderCounterReducers,
selectors: thunderCounterSelectors,
isReadyInSsr: thunderCounterIsReadyInSsr,
} = useKillua(thunderCounter);
return (
<>
<h2> Thunder </h2>
<h2>
Counter: {thunderCounterIsReadyInSsr ? thunderCounterState : 'wait ...'}
</h2>
<hr />
<h2> Set Thunder </h2>
<button onClick={() => thunderCounterSetState((prev) => prev + 1)}>
Set Thunder with callback
</button>
<button onClick={() => thunderCounterSetState(12)}>
Set Thunder without callback
</button>
<hr />
<h2> Reducers </h2>
<button onClick={() => thunderCounterReducers.increment()}>
Increment
</button>
<button onClick={() => thunderCounterReducers.incrementWithPayload(5)}>
Increment with payload
</button>
<button onClick={() => thunderCounterReducers.reset()}>Reset</button>
<hr />
<h2> Selectors </h2>
<button
onClick={() =>
isReadyInSsr
? console.log(thunderCounterSelectors.getCounterPlusOne())
: 'wait ...';
}
>
Get counter with plus one
</button>
<button
onClick={() =>
isReadyInSsr
? console.log(thunderCounterSelectors.getCounterPlusPayload(10))
: 'wait ...';
}
>
Get counter with plus payload
</button>
</>
);
};
export default Counter;