How to pass context consumer to Component (Event.js) rendered by React Router only?react-router - pass props to handler componentHow to pass props onClick in ReactWhy is React Router a React Component?How to get history on react-router v4?How to pass props to this.props.children using React Router 4 with switch?react not found component always rendersHow to render NotFound Component react-router-configReact Router Switch not rendering specific componentReact router render prop route always re-renders componentsTo change the value of the URL in the browser, but not the React component to be rendered
What is the intuition behind uniform continuity?
How to detach yourself from a character you're going to kill?
Future enhancements for the finite element method
Are grass strips more dangerous than tarmac?
How does increase in volume change the speed of reaction in production of NO2?
Explain Ant-Man's "not it" scene from Avengers: Endgame
Beginner's snake game using PyGame
Can you dispel the Slow effect of a Stone Golem?
Modern approach to radio buttons
Looking after a wayward brother in mother's will
Where can I find the list of all tendons in the human body?
Change TeXForm of ArcTan
Can a rogue effectively triple their speed by combining Dash and Ready?
Are there regional foods in Westeros?
Infinitely many hats
How should I push back against my job assigning "homework"?
Is having a hidden directory under /etc safe?
How do I get a list of only the files (not the directories) from a package?
How to read a field in the variant template?
How was Apollo supposed to rendezvous in the case of a lunar abort?
Self-Preservation: How to DM NPCs that Love Living?
The term for the person/group a political party aligns themselves with to appear concerned about the general public
Singlequote and backslash
Why does the UK have more political parties than the US?
How to pass context consumer to Component (Event.js) rendered by React Router only?
react-router - pass props to handler componentHow to pass props onClick in ReactWhy is React Router a React Component?How to get history on react-router v4?How to pass props to this.props.children using React Router 4 with switch?react not found component always rendersHow to render NotFound Component react-router-configReact Router Switch not rendering specific componentReact router render prop route always re-renders componentsTo change the value of the URL in the browser, but not the React component to be rendered
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
My application is using React Router(Router.js), I'm trying to implement CentralStore using Context API to pass state to Event.js component rendered by Router.
How to pass context consumer to Component (Event.js) rendered by React Router only? Right now AppContext is undefined in Event.js.
Live code: https://codesandbox.io/s/0x83zz0jmw
const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
import React from 'react';
import BrowserRouter, Route, Switch from 'react-router-dom';
import App from '../App';
import EventCreator from './EventCreator';
import NotFound from './NotFound';
import Event from './Event';
import Events from './Events';
import CentralStore from '../CentralStore';
const Router = () => (
<CentralStore>
<BrowserRouter>
<Switch>
<Route exact path="/" component=App/>
<Route path="/create-event" component=EventCreator />
<Route path="/events/:eventId" component=Event />
<Route path="/events" component=Events />
<Route component=NotFound/>
</Switch>
</BrowserRouter>
</CentralStore>
);
export default Router;
//
const Event = (props) =>
console.log(props);
return (
<div className="event">
<AppContext.Consumer>
(context) => (
console.log(context)
)
</AppContext.Consumer> */
</div>
);
}
export default Event;
javascript reactjs react-router react-context
add a comment |
My application is using React Router(Router.js), I'm trying to implement CentralStore using Context API to pass state to Event.js component rendered by Router.
How to pass context consumer to Component (Event.js) rendered by React Router only? Right now AppContext is undefined in Event.js.
Live code: https://codesandbox.io/s/0x83zz0jmw
const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
import React from 'react';
import BrowserRouter, Route, Switch from 'react-router-dom';
import App from '../App';
import EventCreator from './EventCreator';
import NotFound from './NotFound';
import Event from './Event';
import Events from './Events';
import CentralStore from '../CentralStore';
const Router = () => (
<CentralStore>
<BrowserRouter>
<Switch>
<Route exact path="/" component=App/>
<Route path="/create-event" component=EventCreator />
<Route path="/events/:eventId" component=Event />
<Route path="/events" component=Events />
<Route component=NotFound/>
</Switch>
</BrowserRouter>
</CentralStore>
);
export default Router;
//
const Event = (props) =>
console.log(props);
return (
<div className="event">
<AppContext.Consumer>
(context) => (
console.log(context)
)
</AppContext.Consumer> */
</div>
);
}
export default Event;
javascript reactjs react-router react-context
did you importAppContext?
– Alexandr Zavalii
Mar 24 at 11:34
I've tried to import it in Event.js as import import AppContext from '../CentralStore' - not working. I'm not exporting AppContext anywhere. I have no idea how to import it.
– Krzysztof Sordyl
Mar 24 at 11:53
I exported AppContext in CentralStore Component, now I have access to state in Event.js file is that good method handling ContextAPI with React Router?
– Krzysztof Sordyl
Mar 24 at 12:47
add a comment |
My application is using React Router(Router.js), I'm trying to implement CentralStore using Context API to pass state to Event.js component rendered by Router.
How to pass context consumer to Component (Event.js) rendered by React Router only? Right now AppContext is undefined in Event.js.
Live code: https://codesandbox.io/s/0x83zz0jmw
const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
import React from 'react';
import BrowserRouter, Route, Switch from 'react-router-dom';
import App from '../App';
import EventCreator from './EventCreator';
import NotFound from './NotFound';
import Event from './Event';
import Events from './Events';
import CentralStore from '../CentralStore';
const Router = () => (
<CentralStore>
<BrowserRouter>
<Switch>
<Route exact path="/" component=App/>
<Route path="/create-event" component=EventCreator />
<Route path="/events/:eventId" component=Event />
<Route path="/events" component=Events />
<Route component=NotFound/>
</Switch>
</BrowserRouter>
</CentralStore>
);
export default Router;
//
const Event = (props) =>
console.log(props);
return (
<div className="event">
<AppContext.Consumer>
(context) => (
console.log(context)
)
</AppContext.Consumer> */
</div>
);
}
export default Event;
javascript reactjs react-router react-context
My application is using React Router(Router.js), I'm trying to implement CentralStore using Context API to pass state to Event.js component rendered by Router.
How to pass context consumer to Component (Event.js) rendered by React Router only? Right now AppContext is undefined in Event.js.
Live code: https://codesandbox.io/s/0x83zz0jmw
const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
import React from 'react';
import BrowserRouter, Route, Switch from 'react-router-dom';
import App from '../App';
import EventCreator from './EventCreator';
import NotFound from './NotFound';
import Event from './Event';
import Events from './Events';
import CentralStore from '../CentralStore';
const Router = () => (
<CentralStore>
<BrowserRouter>
<Switch>
<Route exact path="/" component=App/>
<Route path="/create-event" component=EventCreator />
<Route path="/events/:eventId" component=Event />
<Route path="/events" component=Events />
<Route component=NotFound/>
</Switch>
</BrowserRouter>
</CentralStore>
);
export default Router;
//
const Event = (props) =>
console.log(props);
return (
<div className="event">
<AppContext.Consumer>
(context) => (
console.log(context)
)
</AppContext.Consumer> */
</div>
);
}
export default Event;
javascript reactjs react-router react-context
javascript reactjs react-router react-context
edited Mar 24 at 11:52
Alexandr Zavalii
1,030924
1,030924
asked Mar 24 at 10:53
Krzysztof SordylKrzysztof Sordyl
265
265
did you importAppContext?
– Alexandr Zavalii
Mar 24 at 11:34
I've tried to import it in Event.js as import import AppContext from '../CentralStore' - not working. I'm not exporting AppContext anywhere. I have no idea how to import it.
– Krzysztof Sordyl
Mar 24 at 11:53
I exported AppContext in CentralStore Component, now I have access to state in Event.js file is that good method handling ContextAPI with React Router?
– Krzysztof Sordyl
Mar 24 at 12:47
add a comment |
did you importAppContext?
– Alexandr Zavalii
Mar 24 at 11:34
I've tried to import it in Event.js as import import AppContext from '../CentralStore' - not working. I'm not exporting AppContext anywhere. I have no idea how to import it.
– Krzysztof Sordyl
Mar 24 at 11:53
I exported AppContext in CentralStore Component, now I have access to state in Event.js file is that good method handling ContextAPI with React Router?
– Krzysztof Sordyl
Mar 24 at 12:47
did you import
AppContext ?– Alexandr Zavalii
Mar 24 at 11:34
did you import
AppContext ?– Alexandr Zavalii
Mar 24 at 11:34
I've tried to import it in Event.js as import import AppContext from '../CentralStore' - not working. I'm not exporting AppContext anywhere. I have no idea how to import it.
– Krzysztof Sordyl
Mar 24 at 11:53
I've tried to import it in Event.js as import import AppContext from '../CentralStore' - not working. I'm not exporting AppContext anywhere. I have no idea how to import it.
– Krzysztof Sordyl
Mar 24 at 11:53
I exported AppContext in CentralStore Component, now I have access to state in Event.js file is that good method handling ContextAPI with React Router?
– Krzysztof Sordyl
Mar 24 at 12:47
I exported AppContext in CentralStore Component, now I have access to state in Event.js file is that good method handling ContextAPI with React Router?
– Krzysztof Sordyl
Mar 24 at 12:47
add a comment |
1 Answer
1
active
oldest
votes
Problem was solved by exporting AppContext from CentralStore component
// The missing export
export const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323060%2fhow-to-pass-context-consumer-to-component-event-js-rendered-by-react-router-on%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Problem was solved by exporting AppContext from CentralStore component
// The missing export
export const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
add a comment |
Problem was solved by exporting AppContext from CentralStore component
// The missing export
export const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
add a comment |
Problem was solved by exporting AppContext from CentralStore component
// The missing export
export const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
Problem was solved by exporting AppContext from CentralStore component
// The missing export
export const AppContext = React.createContext();
class CentralStore extends React.Component
state=
events: false,
componentDidMount()
firebase
.collection("events")
.get()
.then( querySnapshot =>
const events = [];
querySnapshot.docs.forEach(doc =>
events.push(doc.data());
);
this.setState(
events: events
);
);
render()
return(
<AppContext.Provider value=
state: this.state
>
this.props.children
</AppContext.Provider>
)
answered Mar 24 at 15:33
Krzysztof SordylKrzysztof Sordyl
265
265
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323060%2fhow-to-pass-context-consumer-to-component-event-js-rendered-by-react-router-on%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
did you import
AppContext?– Alexandr Zavalii
Mar 24 at 11:34
I've tried to import it in Event.js as import import AppContext from '../CentralStore' - not working. I'm not exporting AppContext anywhere. I have no idea how to import it.
– Krzysztof Sordyl
Mar 24 at 11:53
I exported AppContext in CentralStore Component, now I have access to state in Event.js file is that good method handling ContextAPI with React Router?
– Krzysztof Sordyl
Mar 24 at 12:47