Skip to content

Instantly share code, notes, and snippets.

View StMotorSpark's full-sized avatar

Ben Simons StMotorSpark

View GitHub Profile
function useMergeState(initialState) {
const [state, setState] = useState(initialState);
// use useRef to improve functionality when calling the setState asynchronously
const stateRef = useRef(state);
function setRefState(newState) {
stateRef.current = newState;
return setState(newState);
}
function useDataHook() {
const [dataState, setDataState] = useState({
serverData: {},
selections: {}
});
return dataState;
}
function useDisplayHook() {
function useBasicHook() {
const [dataState, setDataState] = useState({
serverData: {},
selections: {}
});
const [viewState, setViewState] = useState({
menuExpanded: false,
submitFormData: {}
})
function useBasicHook() {
const [dataState, setDataState] = useState({
serverData: {},
selections: {}
});
const [viewState, setViewState] = useState({
menuExpanded: false,
submitFormData: {}
})
@StMotorSpark
StMotorSpark / ReactHooksAnimate_animationcomp5.js
Created April 9, 2020 13:46
Adding additional key logic to the animation component to smooth out animations
function useAnimationState(children){
const [animatedItems, setItems] = useState(null);
const itemRef = useRef(animatedItems)
const setAnimatedItems = (newState)=>{
itemRef.current = newState;
return setItems(newState);
}
const priorItem = useRef();
function animationEnd(){
@StMotorSpark
StMotorSpark / ReactHooksAnimate_basicApp2.js
Created April 9, 2020 13:44
Adding keys to the main component to smooth out animations
function MainDisplay(props){
const [cardPos, setCardPos] = useState(0);
var currentCard = <CardDisplay title={props.cards[cardPos].title} text={props.cards[cardPos].text} key={cardPos}/>;
var nextButton = <button onClick={(event)=>{
var nextPos = cardPos + 1;
if(nextPos >= props.cards.length){
nextPos = 0;
}
@StMotorSpark
StMotorSpark / ReactHooksAnimate_animationcomp4.js
Created April 9, 2020 13:42
Adding the fade in css logic to the animation component
function useAnimationState(children){
const [animatedItems, setItems] = useState(null);
const itemRef = useRef(animatedItems)
var refUpdateProxy = {
apply: (target, thisArg, argumentsList) =>{
itemRef.current = argumentsList[0];
return Reflect.apply(target, thisArg, argumentsList);
}
}
const setAnimatedItems = new Proxy(setItems, refUpdateProxy)
@StMotorSpark
StMotorSpark / animateStyle3.css
Created April 9, 2020 13:41
Adding in the fade in animation keyframes
@keyframes animateFadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.fadeIn {
@StMotorSpark
StMotorSpark / ReactHooksAnimate_animationcomp3.js
Created April 9, 2020 13:37
Updating the animation component to use the renders and cleanup after.
function useAnimationState(children){
const [animatedItems, setItems] = useState(null);
function animationEnd(){
// update the animatedItems to remove the first item
setAnimatedItems(itemRef.current.slice(1));
}
useEffect(()=>{
if(animatedItems == null){
@StMotorSpark
StMotorSpark / animateStyle2.css
Created April 9, 2020 13:36
Adding in the keyframe animations
@keyframes animateSlideOut {
0% {
margin-left: 0px;
}
100% {
margin-left: -325px;
}
}
.slideOut {