Created
September 28, 2021 02:01
-
-
Save nameofSEOKWONHONG/9b799735cdf21b29ef0e9b44ddfc776b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi, guys. | |
This article describes the CounterState implementation in react and blazor. | |
```js | |
//react basic code abount counterstate | |
import { Action, Reducer } from 'redux'; | |
// ----------------- | |
// STATE - This defines the type of data maintained in the Redux store. | |
export interface CounterState { | |
count: number; | |
} | |
// ----------------- | |
// ACTIONS - These are serializable (hence replayable) descriptions of state transitions. | |
// They do not themselves have any side-effects; they just describe something that is going to happen. | |
// Use @typeName and isActionType for type detection that works even after serialization/deserialization. | |
export interface IncrementCountAction { type: 'INCREMENT_COUNT' } | |
export interface DecrementCountAction { type: 'DECREMENT_COUNT' } | |
// Declare a 'discriminated union' type. This guarantees that all references to 'type' properties contain one of the | |
// declared type strings (and not any other arbitrary string). | |
export type KnownAction = IncrementCountAction | DecrementCountAction; | |
// ---------------- | |
// ACTION CREATORS - These are functions exposed to UI components that will trigger a state transition. | |
// They don't directly mutate state, but they can have external side-effects (such as loading data). | |
export const actionCreators = { | |
increment: () => ({ type: 'INCREMENT_COUNT' } as IncrementCountAction), | |
decrement: () => ({ type: 'DECREMENT_COUNT' } as DecrementCountAction) | |
}; | |
// ---------------- | |
// REDUCER - For a given state and action, returns the new state. To support time travel, this must not mutate the old state. | |
export const reducer: Reducer<CounterState> = (state: CounterState | undefined, incomingAction: Action): CounterState => { | |
if (state === undefined) { | |
return { count: 0 }; | |
} | |
const action = incomingAction as KnownAction; | |
switch (action.type) { | |
case 'INCREMENT_COUNT': | |
return { count: state.count + 1 }; | |
case 'DECREMENT_COUNT': | |
return { count: state.count - 1 }; | |
default: | |
return state; | |
} | |
}; | |
``` | |
vs | |
```cs | |
//blazor | |
namespace netcoreblazor { | |
public interface ICounterState { | |
int Count {get;} | |
void Increment(); | |
void Decrement(); | |
void SetCount(int num); | |
} | |
public class CounterState : ICounterState { | |
public int Count {get; private set;} | |
public void Increment() { | |
this.Count += 1; | |
} | |
public void Decrement() { | |
this.Count -= 1; | |
} | |
public void SetCount(int num) { | |
this.Count = num; | |
} | |
} | |
} | |
``` | |
```tsx | |
//react | |
import * as React from 'react'; | |
import { connect } from 'react-redux'; | |
import { RouteComponentProps } from 'react-router'; | |
import { ApplicationState } from '../store'; | |
import * as CounterStore from '../store/Counter'; | |
type CounterProps = | |
CounterStore.CounterState & | |
typeof CounterStore.actionCreators & | |
RouteComponentProps<{}>; | |
class Counter extends React.PureComponent<CounterProps> { | |
public render() { | |
return ( | |
<React.Fragment> | |
<h1>Counter</h1> | |
<p>This is a simple example of a React component.</p> | |
<p aria-live="polite">Current count: <strong>{this.props.count}</strong></p> | |
<button type="button" | |
className="btn btn-primary btn-lg" | |
onClick={() => { this.props.increment(); }}> | |
Increment | |
</button> | |
</React.Fragment> | |
); | |
} | |
}; | |
export default connect( | |
(state: ApplicationState) => state.counter, | |
CounterStore.actionCreators | |
)(Counter); | |
``` | |
```razor | |
//blazor | |
@page "/counter" | |
@inject ICounterState counterState | |
<h1>Counter</h1> | |
<p>Current count: @counterState.Count</p> | |
<button class="btn btn-primary" @onclick="@(() => counterState.Increment())">Click me</button> | |
<input type="text" @onchange="@(() => counterState.SetCount(10))"/> | |
@code { | |
} | |
``` | |
```razor | |
//add interface and implement di container | |
//statup.cs > configureservices() | |
services.AddSingleton<ICounterState, CounterState>(); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment