首页 > 解决方案 > 在减速器函数 REACT NATIVE 中将变量包装在 {} 中意味着什么?

问题描述

我是 React native 的新手,我遇到了 reducer 函数的代码,但我对为什么“token”被括在括号中感到困惑。它是否将令牌变成动态的东西或其他东西?

有人可以解释为什么会这样吗?太感谢了!

/** The reducer is in charge of updating the app state based on the dispatched action. **/
//Action Types
export const CREDENTIALED = 'auth/CREDENTIALED';
export const RESET_DATA = 'auth/RESET_DATA';

export const initialState = {
  isLoading: true,
  token: null,
};

//REDUCER
const authReducer = (state = initialState, action) => {
  switch (action.type) {
    case CREDENTIALED: {
      let {token} = action;

      return {...state, isLoading: true, token};
    }

    case RESET_DATA: {
      return {...state, ...initialState};
    }

    default:
      return state;
  }
};

export default authReducer;

标签: react-native

解决方案


无论如何都不是反应本机专家,但看起来令牌只是从行动中解构出来的。大致相同:

const dict = {
  "alpha": 'a',
  "beta": 'b',
}

const {alpha} = dict

console.log(alpha) // you should expect "a" to be printed out

推荐阅读