Introduction to Reduck
Reduck is an innovative library designed to simplify your Redux experience. It provides a streamlined approach to state management in React applications. By using Reduck, developers can focus more on building features and less on boilerplate code.
Key APIs and Code Examples
Below are some essential APIs provided by Reduck along with practical code examples:
configureStore
The configureStore
function helps you create a store effortlessly.
import { configureStore } from 'reduck';
const store = configureStore({
reducer: rootReducer,
preloadedState: initialState,
middleware: [...defaultMiddleware],
});
console.log(store.getState());
createAction
With createAction
, you can define Redux actions conveniently.
import { createAction } from 'reduck';
const increment = createAction('INCREMENT');
const decrement = createAction('DECREMENT');
console.log(increment.type); // 'INCREMENT'
console.log(decrement.type); // 'DECREMENT'
createReducer
The createReducer
function reduces boilerplate code for handling actions in reducers.
import { createReducer } from 'reduck';
const initialState = { value: 0 };
const counterReducer = createReducer(initialState, {
INCREMENT: (state, action) => { state.value += 1 },
DECREMENT: (state, action) => { state.value -= 1 }
});
console.log(counterReducer(undefined, { type: 'INCREMENT' })); // { value: 1 }
createSlice
Use createSlice
to combine actions and reducers in one go.
import { createSlice } from 'reduck';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { state.value += 1 },
decrement: (state) => { state.value -= 1 },
reset: (state) => { state.value = 0 }
}
});
console.log(counterSlice.actions.increment());
App Example Using Reduck
Here’s a simple counter app demonstrating the use of Reduck:
import React from 'react';
import { Provider, useDispatch, useSelector } from 'react-redux';
import { configureStore, createSlice } from 'reduck';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: state => { state.value += 1 },
decrement: state => { state.value -= 1 }
}
});
const { actions, reducer } = counterSlice;
const store = configureStore({ reducer: { counter: reducer } });
const Counter = () => {
const dispatch = useDispatch();
const value = useSelector(state => state.counter.value);
return (
Counter: {value}
);
};
const App = () => (
);
export default App;
As you can see, Reduck makes Redux much simpler and more intuitive.
Start using Reduck today and streamline your Redux development process!
Hash: 97c09be794e11ff8856f5c24d7bad406d949a4cd15626ea9395fc24359776f7f