VK
Size: a a a
VK
EY
const MyComponent: FC = () => {
const store = new MyComponentStore()
return <div>{store.someValue}</div>
}
// Конструктор стора
constructor() {
[this.paginationIsLoading, this.setPaginationIsLoading] = useState<boolean>(false);
[this.lce, this.setLce] = useState<LceState>(LceState.Loading);
[this.table, this.setTable] = ReportPlannedState.useTable();
[this.filters, this.setFilters] = ReportPlannedState.useFilters();
useEffect(() => {
this.componentDidMount()
return this.componentWillUnmount
}, []);
}
private readonly componentDidMount = () => {
this.loadReportFromFirstPage();
window.addEventListener('scroll', this.onScroll)
}
private readonly componentWillUnmount = () => {
window.removeEventListener('scroll', this.onScroll)
}
Мне не нравится, что экземпляр стора в этом случае создаётся на маунте, на анмаунте и на ререндере. В случае классового компонента я его могу спокойно объявить только единожды в конструкторе.И
const MyComponent: FC = () => {
const store = new MyComponentStore()
return <div>{store.someValue}</div>
}
// Конструктор стора
constructor() {
[this.paginationIsLoading, this.setPaginationIsLoading] = useState<boolean>(false);
[this.lce, this.setLce] = useState<LceState>(LceState.Loading);
[this.table, this.setTable] = ReportPlannedState.useTable();
[this.filters, this.setFilters] = ReportPlannedState.useFilters();
useEffect(() => {
this.componentDidMount()
return this.componentWillUnmount
}, []);
}
private readonly componentDidMount = () => {
this.loadReportFromFirstPage();
window.addEventListener('scroll', this.onScroll)
}
private readonly componentWillUnmount = () => {
window.removeEventListener('scroll', this.onScroll)
}
Мне не нравится, что экземпляр стора в этом случае создаётся на маунте, на анмаунте и на ререндере. В случае классового компонента я его могу спокойно объявить только единожды в конструкторе.EY
VK
const MyComponent: FC = () => {
const store = new MyComponentStore()
return <div>{store.someValue}</div>
}
// Конструктор стора
constructor() {
[this.paginationIsLoading, this.setPaginationIsLoading] = useState<boolean>(false);
[this.lce, this.setLce] = useState<LceState>(LceState.Loading);
[this.table, this.setTable] = ReportPlannedState.useTable();
[this.filters, this.setFilters] = ReportPlannedState.useFilters();
useEffect(() => {
this.componentDidMount()
return this.componentWillUnmount
}, []);
}
private readonly componentDidMount = () => {
this.loadReportFromFirstPage();
window.addEventListener('scroll', this.onScroll)
}
private readonly componentWillUnmount = () => {
window.removeEventListener('scroll', this.onScroll)
}
Мне не нравится, что экземпляр стора в этом случае создаётся на маунте, на анмаунте и на ререндере. В случае классового компонента я его могу спокойно объявить только единожды в конструкторе.II
И
const useStore = Store => {
const storeRef = useRef()
useEffect(() => {
storeRef.current = new Store()
}, [])
return storeRef.current
}
const store = useStore(MyStore)EY
AQ
EY
const useStore = Store => {
const storeRef = useRef()
useEffect(() => {
storeRef.current = new Store()
}, [])
return storeRef.current
}
const store = useStore(MyStore)VK
II
EY
И
EY
II
VK