Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Change slick slider slides from another component

import React, { useState, useEffect, useRef } from "react";
import Carousel from "react-slick";
import "slick-carousel/slick/slick.css";

function Parent() {
  const [slideIndex, setSlideIndex] = useState(0);

  return (
    <>
      <Links onClick={setSlideIndex} />
      <Slider slideIndex={slideIndex} />
    </>
  );
}

function Links({ onClick }) {
  return (
    <ul>
      <li>
        <button onClick={() => onClick(0)}>Show Slide 1</button>
      </li>
      <li>
        <button onClick={() => onClick(1)}>Show Slide 2</button>
      </li>
      <li>
        <button onClick={() => onClick(2)}>Show Slide 3</button>
      </li>
      <li>
        <button onClick={() => onClick(3)}>Show Slide 4</button>
      </li>
    </ul>
  );
}

function Slider({ slideIndex }) {
  const slider = useRef();

  useEffect(() => {
    slider.current.slickGoTo(slideIndex);
  }, [slideIndex]);

  return (
    <Carousel ref={slider}>
      <div>Slide 1</div>
      <div>Slide 2</div>
      <div>Slide 3</div>
      <div>Slide 4</div>
    </Carousel>
  );
}
Slider.defaultProps = {
  slideIndex: 0
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: ajax fail function parameters 
Javascript :: jquery find include self 
Javascript :: starting: intent error type 3 react-native 
Javascript :: declare multiple variable javascript 
Javascript :: jquery remove duplicates 
Javascript :: increment number in for loop javascript 
Javascript :: how to multiply two array in javascript 
Javascript :: find leap year javascript 
Javascript :: updating a random variable in a function 
Javascript :: javascript to typescript converter 
Javascript :: ArrayReplace 
Javascript :: js organise string tab spaced 
Javascript :: multply js 
Javascript :: $( ) jquery 
Javascript :: react native pass params to previous screen 
Javascript :: pass a callback funcion into an async function node js 
Javascript :: command for importing fetchgraphqlquery in nextjs 
Javascript :: js how to shuffle array algoritm. The Fisher-Yates algorith 
Javascript :: json format in .net core 
Javascript :: javascript Scroll into a div that is hidden initially in react 
Javascript :: Why is the return function in my debounce function never called? Angularjs 
Javascript :: angularjs Split date and time from api response 
Javascript :: How to hide div based on select the dropdown in angular js 
Javascript :: Delete a field from Firebase Firestore where the field/key has a period/punctuation (".") - modular v9 JavaScript SDK 
Javascript :: coin gecko api 
Javascript :: How do I group values to an array for the same field value in jq 
Javascript :: convert json to string curl 
Javascript :: Printer Errors 
Javascript :: phaser seeded group 
Javascript :: yoptascript 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =