Quick Start Guide for Blazor
This guide will help you set up Ducky in a Blazor application. By following these steps, you'll integrate Ducky for state management and implement a simple counter example.
Create Project & Install Ducky
1. Create a New Blazor Project
First, create a new Blazor WebAssembly project using the .NET CLI:
2. Install Ducky and Ducky.Blazor
Next, install Ducky and the Ducky.Blazor package using the .NET CLI:
Prepare Startup
To configure Ducky in your Blazor application, you'll need to update your Program.cs
file.
1. Configure Ducky in Program.cs
Open the Program.cs
file and add the Ducky services to the Blazor service container.
Basic Configuration
If your slices are automatically discoverable, you can use the basic configuration:
Advanced Configuration
If you want more control, for example, specifying which assemblies to scan for slices, use the following configuration:
Implement a Counter Example
Now that Ducky is set up, let’s implement a simple counter example in your Blazor application.
1. Define Actions
Create a new file called CounterActions.cs
in the Shared
folder:
2. Define Reducers
Create a new file called CounterReducers.cs
in the Shared
folder:
3. Update the Counter Component
Now, let's modify the default Counter.razor
component to use Ducky.
Update Counter.razor
Replace the contents of the Counter.razor
component with the following:
4. Run Your Blazor Application
You can now run your Blazor application using the following command:
5. View the Counter Component
Navigate to /counter
in your web browser. You should see a counter that increments and decrements as you click the respective buttons.
Understanding the Example
Actions: The
Increment
andDecrement
actions are simple records that describe state changes.Reducers: The
CounterReducers
class defines how the state changes in response to the dispatched actions.DuckyComponent: The
Counter.razor
component inherits fromDuckyComponent<int>
, automatically binding the state to the component.Dispatch: The
Dispatch
method is used to send actions to the store, triggering state updates.