小编hug*_*ana的帖子

如何使用 useReducer 更新对象内的数组

我正在尝试更新状态对象。其中一项是一个键,其值是一个字符串数组。

我已经能够更新状态,但它不会重新渲染。

const initialState = {
  showUpload: false,
  listItems: parentDirs,
  uploadPath: [],
  fileList: null
};

const reducer = (state, action) => {
  switch (action.type) {
    case "updateListItems":
      return { ...state, listItems: action.payload };
    case "updatePath":
      return {
        ...state,
        uploadPath: [...state.uploadPath, action.payload]
      };
    default:
      return state;
  }
};

const [state, dispatch] = useReducer(reducer, initialState);

const pathFormat = state.uploadPath.join('/')

return (
 <p>{pathFormat}</p>
)

Run Code Online (Sandbox Code Playgroud)

预期的行为是更新状态对象内的数组将触发重新渲染。

reactjs react-hooks

2
推荐指数
1
解决办法
1万
查看次数

JSX中的混合运算符

我想在JSX中使用混合运算符,例如:

{datas && datas.map(function(data,i){ return <MyComponent key={i} />}) || []}
Run Code Online (Sandbox Code Playgroud)

虽然这在技术上有效,但ES lint警告说"无混合运营商".这是在JSX中使用的安全模式吗?

jsx reactjs

1
推荐指数
1
解决办法
2901
查看次数

标签 统计

reactjs ×2

jsx ×1

react-hooks ×1