在 React 中,以下哪一种方式是定义动作类型的最佳方式?
第一种方式:
使用如下字符串定义操作:
const actionCreatorExample = (value) => {
return { type: 'SET_RESPONSE', value };
}
第二种方式:
在对象中定义动作类型并通过访问该对象的值来引用动作类型。像这样:
export const actionTypes = {
SET_RESPONSE: 'SET_RESPONSE'
};
import actionTypes from './actionTypes';
const actionCreatorExample = (value) => {
return { type: actionTypes.SET_RESPONSE, value };
}
第二种方式看起来很简洁,但为什么要通过存储actionTypes对象来浪费内存呢?有什么想法吗?