Skip to content

Instantly share code, notes, and snippets.

View vldvel's full-sized avatar

Vlad vldvel

  • Amsterdam
View GitHub Profile
const store = createStore(
rootReducer,
compose(applyMiddleware(/** other middlewares */, getWeatherDataMiddleware))
);
const getWeatherDataTimedout = async dispatch => {
await getWeatherData()(dispatch);
setTimeout(() => updateMessages(dispatch), 30 * 1000);
};
const getWeatherDataMiddleware = ({ dispatch }) => next => {
getWeatherDataTimedout(dispatch);
return action => next(action);
@vldvel
vldvel / WeatherAppInitActions.js
Created June 12, 2020 06:34
Creating inital actions in the WeatherApp component to constantly pull weather data.
export const initActions = async dispatch => {
let updateTimeout;
getWeatherDataTimedout = async () => {
await dispatch(getWeatherData());
updateTimeout = setTimeout(getWeatherDataTimedout, 30 * 1000);
}
/** Initialize any other actions */
@vldvel
vldvel / WeatherAppSetTimeout.js
Last active June 12, 2020 06:33
Setting the timeout in the WeatherApp component to constantly pull weather data.
class WeatherApp extends React.Component {
updateTimeout;
componentDidMount() {
this.getWeatherData();
}
getWeatherData = async () => {
await this.props.getWeatherData();
@vldvel
vldvel / WeatherAppSetInterval.js
Created June 11, 2020 06:31
Setting the interval in the WeatherApp component to constantly pull weather data.
class WeatherApp extends Rect.Component {
updateInterval;
componentDidMount() {
this.updateInterval = setInterval(() => {
this.props.getWeatherData();
}, 1000 * 30); // 30 seconds
}
componentWillUnmount() {
const EnumStatus = {
LOADED: 'LOADED',
LOADING: 'LOADING',
FAILED: 'FAILED'
}
const state = {
subState_1: {
data: any,
status: EnumStatus
@vldvel
vldvel / add-middleware-to-store.js
Created February 7, 2019 07:03
add middleware to store
const store = createStore(
rootReducer,
compose(applyMiddleware(/** other middlewares */, updateMessagesMiddleware))
);
@vldvel
vldvel / update-in-middleware.js
Last active February 7, 2019 06:58
update in middleware
const updateMessagesMiddleware = ({ dispatch }) => next => {
const updateMessages = async () => {
await getNewMessages()(dispatch);
setTimeout(updateMessages, 500);
};
updateMessages();
return action => next(action);
};
@vldvel
vldvel / initActions-in-root.js
Last active February 7, 2019 06:47
initActions in root
export const initActions = async dispatch => {
let updateTimeout;
updateMessages = async () => {
await dispatch(getNewMessages());
updateTimeout = setTimeout(updateMessages, 500);
}
updateMessages();
}
@vldvel
vldvel / setTimeout-in-componentDidMount.js
Created February 7, 2019 06:23
setTimeout in componentDidMount
class NewMessages extends React.PureComponent {
updateTimeout;
componentDidMount() {
this.updateMessages();
}
updateMessages = async () => {
// thunk
await this.props.getNewMessages(); // waiting for request