r/reactjs •

Beginner's Thread / Easy Questions (May 2020)

[deleted] — view deleted post

0 Upvotes

401 comments sorted by

View all comments

Show parent comment

1

u/bayhack May 09 '20

It seemed to work but had other issues. I think a hard part I'm having is how createContext() works. Seems like you can't create the Provider and context in the same file? Doesn't also seem exportable, but I can't find anything in the docs.

import React from 'react';
export const SiteThemeContext = React.createContext();
 export class SiteThemeProvider extends React.Component {
    render() {
     return (
        <SiteThemeContext.Provider value={}>
          {this.props.children}
        </SiteThemeContext.Provider>
      );
    }
  }

I don't' get how Provider links to the context from just being exported. As said in this article: https://thelocalhost.blog/2018/09/08/react-context-api/

0

u/[deleted] May 09 '20

[deleted] — view deleted comment

1

u/bayhack May 09 '20

yeah but when I try to set Provider to it's own component like in my code it creates problems. React doesn't view it as a component.

I think I'm missing something very fundamental about React context. It gets very strange when I try to make it a single file and export it out for use. And then when I try to package it to be a reusable NPM component.

0

u/[deleted] May 09 '20

[deleted] — view deleted comment

1

u/bayhack May 09 '20

The value is being passed in the provider? And React throws an error that it expects a component when trying to import and use the Provider

0

u/[deleted] May 09 '20

[deleted] — view deleted comment

1

u/bayhack May 09 '20

oh, your talking about THAT example. I'm talking about my code. My code is all in the StackOverflow post. In my code, I passed a value though.