Hooks and useContext
See original GitHub issueHi 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:
- Created 3 years ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😃Here is the example
Where MyComponentUsingContext is