Skip to content

Instantly share code, notes, and snippets.

View jamesattard's full-sized avatar

James A. jamesattard

View GitHub Profile
@jamesattard
jamesattard / component.js
Created March 28, 2018 22:49
trigger action creator after state completes mutation
class MyComponent extends Component {
render() {
if (!!this.props.payload.success) {
this.props.actionCreator();
return <Redirect to="/dashboard" />;
}
return (
...
)
}
class MyComponent extends Component {
handleNewSubscription = () => {
this.props.actionCreator();
return <Redirect to="/dashboard" />;
};
render() {
!!this.props.payload.success ? this.handleNewSubscription() : null
return (
...
)
class MyComponent extends Component {
componentDidUpdate() {
if (!!this.props.payload.success) {
this.props.actionCreator();
this.props.history.push("/Dashboard");
}
}
render() {
return (
@jamesattard
jamesattard / rename_git_branch.sh
Created April 2, 2018 23:08
Rename git branch
git branch -m old_branch new_branch
git push --set-upstream origin new_branch
git push origin :old_branch
const counter = 5;
[...Array(counter)].forEach(
(_, i) => {
console.log(i);
}
)
@jamesattard
jamesattard / timeRangeMoment.js
Created May 8, 2018 19:53
Create a moment.js time range based on arbitrary duration, start time and end time
const timeRangeArr = [];
const lunchBreakFrom = moment()
.hours(12)
.minutes(0);
const lunchBreakTo = moment()
.hours(13)
.minutes(0);
const duration = 15;
for (
export const fetchPostsAndComments = () => async (
dispatch,
getState
) => {
await dispatch(fetchPostList());
const PostList = await getState().post.postList;
await PostList.forEach(post => {
dispatch(fetchPostComments(post._id));
});
};
...
class PostList extends Component {
async componentDidMount() {
const { fetchPostsAndData } = this.props;
await fetchPostsAndData();
}
renderPosts = () => {
const postList = this.props.postList;
@jamesattard
jamesattard / setstate_async.js
Created July 18, 2018 06:38
Asynchronous setState()
setCoords = coords => {
this.setState({
lat: coords.lat,
lng: coords.lng
});
};
componentDidMount() {
let geocoder = new window.google.maps.Geocoder();
geocoder.geocode(
@jamesattard
jamesattard / noteReducer.js
Created September 1, 2018 07:17
Reducer Architecture
import {
FETCHING_NOTES,
FETCH_NOTES,
FETCH_NEW_NOTE,
FETCH_UPDATED_NOTE,
FETCH_CLOSED_NOTE
} from "../actions/types";
const initialState = {
list: [],