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 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 })); |
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 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; |
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 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); | |
}; |
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
// 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; |
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
// 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; |
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
// 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'; | |
} |
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
// pages/post.js | |
import React from 'react'; | |
import { getPost } from '../data/posts'; | |
const Post = ({ statusCode, post }) => { | |
if (statusCode !== 200) { | |
return ( | |
<div> |
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
// 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, |
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
// pages/post.js | |
import React from 'react'; | |
const Post = ({ slug }) => <div>{slug}</div>; | |
Post.getInitialProps = async ({ query }) => { | |
const { slug } = query; | |
return { |
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
{ | |
"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" } |
NewerOlder