question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hooks and useContext

See original GitHub issue

Hi all,

Thanks for doing this project!

Am I missing something or the usage CarouselContext with hooks aren’t supported yet?

I follow the example on the README file, but it looks like CarouselContext it’s empty: https://github.com/express-labs/pure-react-carousel/blob/master/src/CarouselProvider/context.js#L3

I did exactly as it says:

import React, { useContext, useEffect, useState } from 'react';
import { CarouselContext } from 'pure-react-carousel';

export function MyComponentUsingContext() {
  const carouselContext = useContext(CarouselContext);
  const [currentSlide, setCurrentSlide] = useState(carouselContext.state.currentSlide);
  useEffect(() => {
    function onChange() {
      setCurrentSlide(carouselContext.state.currentSlide);
    }
    carouselContext.subscribe(onChange);
    return () => carouselContext.unsubscribe(onChange);
  }, [carouselContext]);
  return `The current slide is: ${currentSlide}`;
}

But this constant carouseContext is returning undefined:

const carouselContext = useContext(CarouselContext); // it returns undefined

Any ideas why?

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
misshu1commented, Aug 1, 2020

Hello, in my opinion, the problem is with the <CarouselProvider /> you can’t use the context and the provider in the same component, move the provider one level up, that should work 😃

2reactions
theone3nucommented, Dec 17, 2020

Here is the example

`  
const Carousel = ({ schools, onSchoolHover }) => {
    
    return (
        <CarouselProvider
            totalSlides={schools.length}
            naturalSlideWidth={1}
            naturalSlideHeight={1}
        >
            <Slider>
                {schools.map((school, index) => {
                    return (
                        <Slide className="custom-slider-height" index={index}>
                           
                        </Slide>
                    )
                }
                )}

            </Slider>
                    <MyComponentUsingContext onSliderChange={onSchoolHover }/>
           
        </CarouselProvider>
    )
}

export default Carousel;`

Where MyComponentUsingContext is

import React, { useContext, useEffect, useState } from 'react';
import { CarouselContext } from 'pure-react-carousel';

export function MyComponentUsingContext({ onSliderChange }) {
  const carouselContext = useContext(CarouselContext);
  const [currentSlide, setCurrentSlide] = useState(carouselContext.state.currentSlide);
  useEffect(() => {
    function onChange() {
      setCurrentSlide(carouselContext.state.currentSlide);
     if(currentSlide !== carouselContext.state.currentSlide) onSliderChange(carouselContext.state.currentSlide)
    }
    carouselContext.subscribe(onChange);
    return () => carouselContext.unsubscribe(onChange);
  }, [carouselContext]);
  return `The current slide is: ${currentSlide}`;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

React useContext Hook - W3Schools
React Context is a way to manage state globally. It can be used together with the useState Hook to share state between deeply...
Read more >
Hooks API Reference - React
This page describes the APIs for the built-in Hooks in React. ... If you're familiar with the context API before Hooks, useContext(MyContext) is...
Read more >
A Guide to React Context and useContext() Hook
The React context provides data to components no matter how deep they are in the components tree. The context is used to manage...
Read more >
How to use “useContext” in React Hooks | by Mayank Gupta
“useContext” hook is used to create common data that can be accessed throughout the component hierarchy without passing the props down manually to...
Read more >
React useContext Hook Tutorial (with Examples) - Dave Ceddia
React's useContext hook makes it easy to pass data throughout your app without manually passing props down the tree.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found