作者:晕晕的劳拉 | 来源:互联网 | 2023-09-17 15:32
我有一个组件,单击按钮后,该函数将更新状态(顺序),该状态将重新呈现组件,但仅更新跨度的文本。是否也应该重新渲染“ i标签”?
const {useState,useEffect} = React;
const SortType = {
UNSORTERED: { icon: 'fas fa-sort',next: 'ASCENDING' },ASCENDING: { icon: 'fas fa-sort-up',next: 'DESCENDING' },DESCENDING: { icon: 'fas fa-sort-down',next: 'UNSORTERED' }
}
const Sorter = ({ trigger,sortOrder }) => {
const [order,setOrder] = useState(SortType.UNSORTERED)
function onSort() {
const nSortOrder = SortType[order.next]
setOrder(nSortOrder)
trigger(nSortOrder)
}
useEffect(() => {
if (sortOrder) {
setOrder(sortOrder)
}
},[sortOrder])
return (
)
}
ReactDOM.render(
,document.getElementById("react")
);