Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
redux-toolkit
Advanced tools
提供实用的工具函数,改善使用redux的开发体验,提供代码可读性。
createReducer
a functional way to write reducercreateAction
a simple way to write actiondebugMiddleware
a useful debug middlewaremnpm install redux-toolkit
import { createAction, createReducer, debugMiddleware } from 'redux-toolkit'
避免使用switch碰到的问题:
case
下的变量冲突问题action
和 state
break
和 default
恶心下面是一个简单的reducer例子
import { ADD_TODO, DELETE_TODO, EDIT_TODO, COMPLETE_TODO, COMPLETE_ALL, CLEAR_COMPLETED } from '../constants/ActionTypes';
const initialState = [{
text: 'Use Redux',
completed: false,
id: 0
}];
export default createReducer({
[ADD_TODO]: (state, { text }) => [{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text
}, ...state],
[DELETE_TODO]: (state, { id }) => state.filter(todo =>
todo.id !== action.id
),
[EDIT_TODO]: (state, { id, text }) => state.map(todo =>
todo.id === id ?
Object.assign({}, todo, { text }) :
todo
),
[COMPLETE_ALL]: state => {
const areAllMarked = state.every(todo => todo.completed);
return state.map(todo => Object.assign({}, todo, {
completed: !areAllMarked
}));
},
[CLEAR_COMPLETED]: state => state.filter(todo => todo.completed === false)
}, initialState)
提供更简单的方法去创建actionCreator。下面是通过actionCreator和普通方法进行对比。
创建没有payload的action
createAction('showAll');
function() {
return {
type: 'showAll'
}
}
只有一个携带值
当只有一个需要传递给reducer
的值时,接受一个key。
createAction('add', 'value');
function(value) {
return {
type: 'add',
payload: {
value: value
}
};
}
传递多个值
接受一个keys数组,会将参数按顺序放置在action
的payload
属性中。
createAction('add', ['num1', 'num2']);
function (num1, num2) {
return {
type: 'add',
payload: {
num1: num1,
num2: num2
}
};
}
根据函数创建action
接受一个将参数处理为payload
的函数
createAction('add', (num1, num2) => {
number1: num1,
number2: num2,
other: num1 * num2
})
function (num1, num2) {
var getAction = (num1, num2) => {
number1: num1,
number2: num2,
other: num1 * num2
};
return {
type: 'add',
payload: getAction(num1, num2)
};
}
提供一个debug的middleware
FAQs
supply useful utils for redux
The npm package redux-toolkit receives a total of 1,943 weekly downloads. As such, redux-toolkit popularity was classified as popular.
We found that redux-toolkit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.