a bit of types
This commit is contained in:
parent
fd0eb66563
commit
0f211115cf
@ -1,5 +1,5 @@
|
|||||||
export const createStore = (initialState) => {
|
export const createStore = <StateType>(initialState: StateType) => {
|
||||||
let state = initialState
|
let state: StateType = initialState
|
||||||
const listeners = new Set<() => void>()
|
const listeners = new Set<() => void>()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -149,9 +149,8 @@ const withStars =
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const Counter = ({ stars, addStar, subStar, userId }) => {
|
const Counter = ({ stars, addStar, subStar, userId }: { userId: 'some-user-id' | '2'}) => {
|
||||||
const { rate, setUserRate } = useUsers((state) => state[userId].rated)
|
const { rate, setUserRate } = useUsers((state) => state[userId].rated)
|
||||||
console.log(userId)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Center>
|
<Center>
|
||||||
|
@ -25,7 +25,7 @@ export const Stars = ({
|
|||||||
userId,
|
userId,
|
||||||
}: {
|
}: {
|
||||||
count: number
|
count: number
|
||||||
userId: string
|
userId: 'some-user-id' | '2'
|
||||||
// rated: number
|
// rated: number
|
||||||
// setRated: (rate: number) => void
|
// setRated: (rate: number) => void
|
||||||
}) => {
|
}) => {
|
||||||
|
16
src/hooks.ts
16
src/hooks.ts
@ -2,16 +2,16 @@ import { useEffect, useState, useSyncExternalStore } from 'react'
|
|||||||
|
|
||||||
import { usersStore } from './__data__/users'
|
import { usersStore } from './__data__/users'
|
||||||
|
|
||||||
export const useUsers = (selector) => {
|
export const useUsers = <StatePart>(selector: (state: ReturnType<typeof usersStore.getState>) => StatePart) => {
|
||||||
// const [rate, setRate] = useState(selector(usersStore.getState()))
|
// const [rate, setRate] = useState(selector(usersStore.getState()))
|
||||||
|
|
||||||
// useEffect(() => {
|
// useEffect(() => {
|
||||||
// const unsubscribe = usersStore.subscribe(() => {
|
// const unsubscribe = usersStore.subscribe(() => {
|
||||||
// setRate(selector(usersStore.getState()))
|
// setRate(selector(usersStore.getState()))
|
||||||
// })
|
// })
|
||||||
|
|
||||||
// return unsubscribe
|
// return unsubscribe
|
||||||
// }, [])
|
// }, [])
|
||||||
|
|
||||||
const rate = useSyncExternalStore(usersStore.subscribe, () =>
|
const rate = useSyncExternalStore(usersStore.subscribe, () =>
|
||||||
selector(usersStore.getState()),
|
selector(usersStore.getState()),
|
||||||
|
Loading…
Reference in New Issue
Block a user