Skip to content

Instantly share code, notes, and snippets.

@DZuz14
DZuz14 / Slider.js
Last active February 8, 2020 23:58
Slider with React Hooks 4
/** @jsx jsx */
import React, { useState } from 'react'
import { css, jsx } from '@emotion/core'
import SliderContent from './SliderContent'
/**
* @function Slider
*/
const Slider = () => {
const getWidth = () => window.innerWidth
@DZuz14
DZuz14 / Slide.js
Last active February 18, 2020 17:50
Slider with React Hooks 5
/** @jsx jsx */
import React from 'react'
import { css, jsx } from '@emotion/core'
const Slide = ({ content }) => (
<div
css={css`
height: 100;
width: 100%;
background-image: url('${content}');
@DZuz14
DZuz14 / index.js
Last active February 9, 2020 00:01
Slider with React Hooks 6
import React from 'react'
import ReactDOM from 'react-dom'
import Slider from './components/Slider'
const images = [
'https://images.unsplash.com/photo-1449034446853-66c86144b0ad?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80',
'https://images.unsplash.com/photo-1470341223622-1019832be824?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2288&q=80',
'https://images.unsplash.com/photo-1448630360428-65456885c650?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2094&q=80',
'https://images.unsplash.com/photo-1534161308652-fdfcf10f62c4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2174&q=80'
]
@DZuz14
DZuz14 / Slider.js
Last active October 4, 2021 19:13
Slider With React Hooks 7
/** @jsx jsx */
import React, { useState, useEffect, useRef } from 'react'
import { css, jsx } from '@emotion/core'
import SliderContent from './SliderContent'
import Slide from './Slide'
/**
* @function Slider
*/
const Slider = props => {
@DZuz14
DZuz14 / Arrow.js
Created February 8, 2020 22:33
Slider with React Hooks 8
/** @jsx jsx */
import React from 'react'
import { css, jsx } from '@emotion/core'
import leftArrow from '../img/left-arrow.svg'
import rightArrow from '../img/right-arrow.svg'
const Arrow = ({ direction, handleClick }) => (
<div
onClick={handleClick}
css={css`
@DZuz14
DZuz14 / Dots.js
Created February 8, 2020 22:34
Slider with React Hooks 9
/** @jsx jsx */
import React from 'react'
import { css, jsx } from '@emotion/core'
const Dot = ({ active }) => (
<span
css={css`
padding: 10px;
margin-right: 5px;
cursor: pointer;
@DZuz14
DZuz14 / Slider.js
Last active February 9, 2020 00:19
Slider with React Hooks 10
/** @jsx jsx */
import React, { useState, useEffect, useRef } from 'react'
import { css, jsx } from '@emotion/core'
import SliderContent from './SliderContent'
import Slide from './Slide'
import Arrow from './Arrow'
/**
* @function Slider
*/
@DZuz14
DZuz14 / Slider.js
Created February 9, 2020 00:19
Slider with React Hooks 11
/** @jsx jsx */
import React, { useState, useEffect, useRef } from 'react'
import { css, jsx } from '@emotion/core'
import SliderContent from './SliderContent'
import Slide from './Slide'
import Arrow from './Arrow'
import Dots from './Dots'
/**
* @function Slider
@DZuz14
DZuz14 / Slider.js
Created March 1, 2020 21:13
useRef and useEffect in tandem
import React, { useState, useEffect, useRef } from 'react'
const autoPlayRef = useRef()
useEffect(() => {
autoPlayRef.current = nextSlide
})
...rest of Slider component
@DZuz14
DZuz14 / Slider.js
Created March 1, 2020 21:24
useEffect on initial render in Slider
useEffect(() => {
const play = () => {
autoPlayRef.current()
}
const interval = setInterval(play, props.autoPlay * 1000)
return () => clearInterval(interval)
}, [])
...rest of Slider component