我正在尝试更新状态对象。其中一项是一个键,其值是一个字符串数组。
我已经能够更新状态,但它不会重新渲染。
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)
预期的行为是更新状态对象内的数组将触发重新渲染。
我想在JSX中使用混合运算符,例如:
{datas && datas.map(function(data,i){ return <MyComponent key={i} />}) || []}
Run Code Online (Sandbox Code Playgroud)
虽然这在技术上有效,但ES lint警告说"无混合运营商".这是在JSX中使用的安全模式吗?