Skip to content

Instantly share code, notes, and snippets.

View robbertvancaem's full-sized avatar
👋
Hi!

Robbert van Caem robbertvancaem

👋
Hi!
View GitHub Profile
@robbertvancaem
robbertvancaem / parallax.js
Last active December 9, 2019 22:11
parallax-spring-3
import React, { useEffect, useRef } from 'react';
import { animated, useSpring } from 'react-spring';
// You can use this `calc` method to increase the impact
// of the effect by playing around with the values and units.
const calc = o => `translateY(${o * 0.1}px)`;
const Comp = () => {
const ref = useRef();
const [{ offset }, set] = useSpring(() => ({ offset: 0 }));
@robbertvancaem
robbertvancaem / parallax.js
Last active December 9, 2019 22:13
parallax-spring-2
import React, { useEffect, useRef } from 'react';
import { useSpring } from 'react-spring';
const Comp = () => {
const ref = useRef();
const [{ offset }, set] = useSpring(() => ({ offset: 0 }));
const handleScroll = () => {
const posY = ref.current.getBoundingClientRect().top;
const offset = window.pageYOffset - posY;
@robbertvancaem
robbertvancaem / parallax.js
Last active December 9, 2019 22:13
parallax-spring-1
import React, { useEffect, useRef } from 'react';
const Comp = () => {
const ref = useRef();
const handleScroll = () => {
const posY = ref.current.getBoundingClientRect().top;
const offset = window.pageYOffset - posY;
console.log(offset);
};
@robbertvancaem
robbertvancaem / post.js
Created December 9, 2019 08:32
dynamic-routing-now-8
// pages/post.js
import React from 'react';
import withError from '../hoc/withError';
import { getPost } from '../data/posts';
const Post = ({ post }) => {
const { title: { rendered: title }, slug } = post;
@robbertvancaem
robbertvancaem / withError.js
Last active December 9, 2019 08:32
dynamic-routing-now-7
// hoc/withError.js
import React from 'react';
import Error from '../pages/_error';
export default Component => class extends React.Component {
static async getInitialProps(ctx) {
const props = await Component.getInitialProps(ctx);
const { statusCode } = props;
@robbertvancaem
robbertvancaem / _error.js
Created December 9, 2019 08:31
dynamic-routing-now-6
// pages/_error.js
import React from 'react';
const Error = ({ statusCode }) => {
let errorMessage = 'An unexpected error occured';
if (statusCode === 404) {
errorMessage = 'Page could not be found';
}
@robbertvancaem
robbertvancaem / post.js
Created December 9, 2019 08:31
dynamic-routing-now-5
// pages/post.js
import React from 'react';
import { getPost } from '../data/posts';
const Post = ({ statusCode, post }) => {
if (statusCode !== 200) {
return (
<div>
@robbertvancaem
robbertvancaem / posts.js
Created December 9, 2019 08:30
dynamic-routing-now-4
// data/posts.js
import axios from 'axios';
export const getPost = async (slug) => {
const response = await axios.get(`https://www.mywordpress.com/wp-json/wp/v2/posts?slug=${slug}`);
if (!response.data.length) {
return {
statusCode: 404,
@robbertvancaem
robbertvancaem / post.js
Created December 9, 2019 08:30
dynamic-routing-now-3
// pages/post.js
import React from 'react';
const Post = ({ slug }) => <div>{slug}</div>;
Post.getInitialProps = async ({ query }) => {
const { slug } = query;
return {
@robbertvancaem
robbertvancaem / now.json
Created December 9, 2019 08:29
dynamic-routing-now-2
{
"name": "dynamic-routing",
"version": 2,
"builds": [{ "src": "next.config.js", "use": "@now/next" }],
"routes": [
{ "src": "/", "dest": "/index" },
{ "src": "/(?<slug>[^/]+)", "dest": "/post?slug=$slug" },
{
"src": "/_next/static/(?:[^/]+/pages|chunks|runtime)/.+",
"headers": { "cache-control": "immutable,max-age=31536000" }