A Simple App with Redux and React
I was in the middle of refactoring the VocaloidInfo NetEase Cloud Music plugin, which uses TypeScript + React, so I figured I'd pull it out and write a quick post about it.
The requirements were like this:
- When the user opens a song page, fetch data from VocaDB. Since fetching takes some time, show a line of text before the result comes back.
- Once the result comes back, update that text with the data.
- Refresh the data when the user switches songs.
The text display part is handled by a React component, but I need to update the data inside it, so I used Redux to do a simple update.
The Redux part
Action
An action can be explained as a behavior that changes data (state). It has two properties, type and data. In theory, we should use the reducer to check the action's type and then decide how to update state with data.
For example, for an int state with two types, add and minus. If the type is add, add data to the state; if the type is minus, subtract data from the state.
Reducer
A reducer is a function that takes state and action and returns the new state.
In the reducer, we need to check the action's type, then process the state using the action's data, and return the new state.
Store
The store is Redux's data container. It's responsible for storing state. When its dispatch method is called, it updates state through its own reducer and the action passed to the dispatch method.
In practice
Here I need to implement a store for song info.
Because my requirements are a bit special: the song data actually only takes a very short time, and after switching songs the old data is useless. Plus my React component needs both the type and data from the action, so I decided to throw out state entirely and just use the action as the state for the React component.
The code is as follows:
import { configureStore, Action } from "@reduxjs/toolkit"; // import
// Here we define three types for SongAction
export enum SongActionType {
LOADING, // Song data is loading
LOADED, // Song data has loaded
FAILED, // Song data not found
ALL = LOADING | LOADED | FAILED
}
// Define SongAction
export interface SongAction extends Action {
type: SongActionType,
data: {
vocadbData: any, // VocaDB data for the song
bilibiliData?: any // Bilibili data for the song
}
}
// Define the reducer
const reducer = ( _, action: SongAction) => { // The underscore in the first parameter should actually be state, but I don't need it, so I threw it away and just pass the original action through
return action;
}
// Create the store
export const SongInfoStore = configureStore({ reducer: reducer });
That's it for the Redux part.
The React part
The React side needs to subscribe to the store to update data. Code below:
// React component for song data
export default abstract class DefaultSongInfo extends React.Component<{store: Store<SongAction> }> {
// Subscribe to the store via its subscribe method to update
componentDidMount(): void {
this.props.store.subscribe(() => {
this.forceUpdate();
});
}
render() {
const store = this.props.store;
const action = store.getState();
// Next is rendering based on the action; not going into detail
}
}
To render this component, use the following code (ReactDOM):
ReactDOM.render(<DefaultSongInfo { ...{store: SongInfoStore} }>, 挂载点);
Updating data
Use the dispatch method to update data:
Before fetching data:
SongInfoStore.dispatch( {type: SongActionType.LOADING } );
Successfully fetched data:
SongInfoStore.dispatch( { type: SongActionType.LOADED , data: data } )
If it failed:
SongInfoStore.dispatch( {type: SongActionType.FAILED } );
