42
loading...
This website collects cookies to deliver better user experience
atoms
) that can be structured as preferred and dynamically bound to any UI-Component for reactivity?createState('Hello World');
) while providing a powerful toolset focused on developer experience around those States.npm
or yarn
. To properly work with AgileTs in a React environment, we need to add two different packages to our existing React application.😎 If you want to set up a project from scratch, you can also use the official create-react-app
template for AgileTs.
// Javascript
npx create-react-app my-app --template agile
// Typescript
npx create-react-app my-app --template agile-typescript
npm install @agile-ts/core
core
package contains the state management logic of AgileTs and therefore offers powerful classes such as the State Class
.npm install @agile-ts/react
useAgile()
Hook to easily bind States to React Components for reactivity.const MY_FIRST_STATE = createState("Hello World");
createState()
and specify an initial value.const RandomComponent = () => {
const myFirstState = useAgile(MY_FIRST_STATE); // <-
return (
<div>
<p>{myFirstState}</p>
</div>
);
}
// <-
) we bind our just created State to the React Component ('RandomComponent') using the useAgile()
Hook. This binding ensures that the Component re-renders whenever the State value
mutates. The useAgile()
Hook returns the current value
of the State. So in our case, something like 'Hello World'.MY_FIRST_STATE.set(`Hello World ${++helloWorldCount}`);
value
with the help of the State's .set()
function on each 'Update State' button press. Thereby we increase the external set helloWorldCount
in ascending order.// 2️⃣ Create State with the initial value "Hello World"
const MY_FIRST_STATE = App.createState("Hello World");
let helloWorldCount = 0;
const RandomComponent = () => {
// 3️⃣ Bind initialized State to the 'RandomComponent' for reactivity
const myFirstState = useAgile(MY_FIRST_STATE);
return (
<div>
<p>{myFirstState}</p>
<button
onClick={() => {
// 4️⃣ Update State value on Button press
MY_FIRST_STATE.set(`Hello World ${++helloWorldCount}`);
}}
>
Update State
</button>
</div>
);
}
counter-example
as in the React example section above:atomic
State Management Libraries like Recoil. States in AgileTs are created individually and lay above the UI-Layer, while they can be dynamically bound to any UI-Component (for example via Hooks).// Update State value to 'hi'
MY_STATE.set('hi');
// Undo latest State value change
MY_STATE.undo();
// Check if the State value is equal to '{hello: "jeff"}'
MY_STATE.is({hello: "jeff"});
// Reset State to its intial value
MY_STATE.reset();
// Preserves the State `value` in the corresponding external Storage
MY_STATE.persist();
// Update State value in 200ms intervals
MY_STATE.interval((value) => value++, 200);
const INTRODUCTION= App.createComputed(() => {
return `Hello I am '${MY_NAME.vale}'.`;
});
MY_NAME
changes from 'jeff' to 'hans'.INTRODUCTION.value; // Returns "Hello I am 'jeff'."
MY_NAME.set('hans');
INTRODUCTION.value; // Returns "Hello I am 'hans'."
data objects
following the same pattern. Each of these data objects requires a unique item key
to be correctly identified later. Think of a Collection like a database table that stores a data object once keyed by an id (item key
).const JOKES = App.createCollection();
JOKES.collect({
id: 1,
joke: "Why do Java programmers have to wear glasses?\n
Because they don't C#"
}, ['programming']);
programming
Group. Groups allow us to easily cluster together data from a Collection as an array of item keys.JOKES.getGroup('chucknorris').value; // Returns Chuck Norris Jokes
JOKES.getGroup('programming').value; // Returns Programming Jokes
JOKES.getDefaultGroup().value; // Returns All Jokes
// Component re-renders only when 'user.name' mutates
const name = useSelector(MY_USER, (value) => value.name);
console.log(name); // Returns 'jeff'
// Component re-renders only when 'user.age' mutates
const user = useProxy(MY_USER);
console.log(user.age); // Returns '8'
dev tools
yet.globalThis
const MY_STATE = createState('jeff');
const MY_COLLECTION = createCollection();
globalBind('__core__', {
MY_STATE,
MY_COLLECTION
});
core
of the AgileTs documentation is globally bound for better debugging. globalThis
in production because then third parties can easily interfere in your internal application logic. Since the AgileTs documentation has no vulnerable logic under the hood, the core
is also accessible in production. Thus you can play around with the AgileTs documentation core
and, for example, update the NPM_DOWNLOADS
State or update the astronaut color.__core__.stats.NPM_DOWNLOADS.set(999999);
useProxy()
hook.