Created
July 6, 2019 07:10
-
-
Save konstantin24121/944364fd503b55899239bb937fb84d5d to your computer and use it in GitHub Desktop.
Пример с react-stores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class UserActionsClass { | |
public async requestProfile(): void { | |
const result = await api.send('profile', 'user', {}); | |
this.updateProfile(); | |
} | |
private updateProfile(data) { | |
// convert data to userStore acceptable view | |
newProfile = ... | |
// | |
balance = this.convertBalance(data.balance) | |
// Save data to store | |
UserStore.store.setState({ | |
profile: newProfile, | |
loaded: true, | |
balance: balance, | |
}); | |
} | |
// Some calculatable value based on store | |
private isNoAddress(): boolean { | |
const profile = UserStore.store.state.profile; | |
return !profile.address || !profile.zip || !profile.city; | |
} | |
// Actualy you can create a memoized function here | |
// If its hard operation | |
private isNoAddress = (): boolean { | |
const profile = UserStore.store.state.profile; | |
return memoize((profile.address, profile.zip, profile.city) => !profile.address || !profile.zip || !profile.city) | |
} | |
} | |
export const UserActions = new UserActionsClass(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can use stores with Component or with FunctionComponent | |
export class UserPage extends React.Component() { | |
async componentDidMount() { | |
// You can do this in you component | |
this.setState({ loading: true }) | |
await UserActions.requestProfile(); | |
this.setState({loading: false}); | |
this.userEvent = UserStore.store.on('update', (state) => { | |
this.setState({ | |
profile: state.profile, // link profile to you component | |
}) | |
}) | |
} | |
componentWillUnmount() { | |
this.userEvent.remove() // Remove subscribe | |
} | |
render() { | |
return (...) | |
} | |
} | |
// OR | |
export const UserPage = () => { | |
const [ loading, setLoading ] = React.useState(false); | |
const profile = useStore(UserStore.store, { | |
mapState: (store) => store.profile | |
}) | |
React.useEffect(() => { | |
async function fetchProfile() { | |
setLoading(true) | |
await UserActions.requestProfile(); | |
setLoading(false) | |
} | |
fetchProfile(); | |
}, []) | |
return (...) | |
} | |
// You also can make a hook with important data which you using all the time | |
const useProfile () => { | |
return useStore(UserStore.store, { | |
mapState: (store) => store.profile | |
}) | |
} | |
// and use it in components | |
export const SomeComponentWhichUseProfile = () => { | |
const profile = useProfile(); | |
return <>{profile.name}</>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Создаем хранилище и описываем его структуру | |
import { Store } from 'react-stores'; | |
export interface Profile { | |
id: number; | |
regtime: Date; | |
email: string; | |
name: string; | |
surname: string; | |
dummyName: string; | |
gender: number; | |
birthdate: Date; | |
lang: string; | |
} | |
const initialState: State = { | |
// Loaded may need but not LOADING state | |
loaded: false, | |
balance: 0, | |
profile: <Profile>{ | |
id: 0, | |
regtime: new Date(0), | |
email: '', | |
name: '', | |
surname: '', | |
dummyName: '', | |
gender: CONFIG.ENUMS.PROFILE.GENDER.MALE, | |
birthdate: new Date(0), | |
lang: '', | |
}, | |
} | |
export const store: Store<State> = new Store<State>(initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment