React Context Provide not passing the value to the Consumer - reactjs

I am having the below Provide which contains the authentication state in it.
export const AuthenticationContext = React.createContext();
export default class AuthenticationProvider extends React.Component {
state = {
isAuthenticated: false
};
render() {
return (
<AuthenticationContext.Provider value={{ state: this.state }}>
{this.props.children}
</AuthenticationContext.Provider>
);
}
}
I have this wrapped to my Routes as below:
<Provider store={store}>
<ConnectedRouter history={history}>
<>
<GlobalStyle />
<AuthenticationProvider>
<SiteHeader />
<ErrorWrapper />
<Switch>
<PrivateHomeRoute exact path="/" component={Home} />
<Route exact path="/login" component={LoginPage}
</Switch>
</AuthenticationProvider>
</>
</ConnectedRouter>
</Provider>
But when I am trying to get context state into the <SiteHeader />, there is nothing passed down by the Context.Provide. My Cosumer is inside <SiteHeader /> is:
class SiteHeader extends React.Component {
render() {
return (
<div>
<AuthenticationContext.Consumer>
{context => (
<header>this is header {context.state.isAuthenticated}</header>
)}
</AuthenticationContext.Consumer>
</div>
);
}
I checked the React devtools, but it's same. Context.Consumer doesn't have value prop from the Provider.
What might be the issue here?

If a value is false, null or undefined, it will not render. I tried your code in a sandbox here. Just added the .toString() in the header, and the value of the boolean is shown in the header.

Context Consumers don't get any data as a props.
instead we pass them a render prop, in this case the children that we pass is the render prop function. and then in the render method of the Consumer something like this happens
render(){
this.props.children(value)
}
this is how we get the value as an argument of the render prop function.
The value of the context provider is not supposed to be passed in through props. You can learn more about render props here

Related

Redux how to get updated store state in root component

My root App component looks like this:
class App extends Component {
render() {
const state = store.getState();
const { isAuthenticated } = state.auth;
return (
<Provider store={store}>
<BrowserRouter basename="/">
<Switch>
{isAuthenticated ? (
<PrivateRoute
exact
path="/library/:user_id"
component={RessourcesByUser}
/>
) : (
<PublicRoute
exact
path="/library/:user_id"
component={PublicRessourcesByUserPage}
/>
)}
</Switch>
</BrowserRouter>
</Provider>
);
}
}
The problem with isAuthenticated is that when the app is first loaded before the user logs in its value is false.
And it makes sense since the user has not logged in yet.
The problem is that when he logs in, the App component does not mount again (obviously), nor does it get the updated store state in which isAuthenticated is true.
So isAuthenticated here in App component will remain false even though the user is authenticated and its value in the store is true.
It will change to true here once he refreshes because App component will get the updated state then in which isAuthenticated is true.
However, meanwhile this will cause logical bugs such as when he goes to a user's Library right after loading the app and logging-in the first time by clicking on a button that would direct to this path /library/:user_id, he will see the public component PublicRessourcesByUserPage that is meant to be displayed for non-authenticated users instead of RessourcesByUser which is meant for authenticated users.
It's been a while since I've used react-router, but if you want to get the freshest (reactive) state with Redux, then your component will need to be connected to Redux. So something along these lines:
import { connect } from 'react-redux';
class AuthRoutes extends Component {
render() {
return (
<Switch>
{this.props.isAuthenticated ? (
<PrivateRoute
exact
path="/library/:user_id"
component={RessourcesByUser}
/>
) : (
<PublicRoute
exact
path="/library/:user_id"
component={PublicRessourcesByUserPage}
/>
)}
</Switch>
)
}
}
connect((state) => ({ isAuthenticated: state.isAuthenticated }),null)(AuthRoutes);
---
class App extends Component {
render() {
return (
<Provider store={store}>
<BrowserRouter basename="/">
<AuthRoutes />
</BrowserRouter>
</Provider>
);
}
}
Note: I'm making an assumption that Switch will work OK like this.

Effecting different component's state in React

I have two different components in React "Header" and "Main".
Header:
import React, { Component } from 'react'
import Logo from './HeaderComps/Logo'
import UserHeader from './HeaderComps/UserHeader'
export default class Header extends Component {
render() {
return (
<header>
<Logo />
<UserHeader name ="Boris"/>
</header>
)
}
}
Main:
export default class Main extends Component {
state ={isLogged : false}
handleClientFirstImpact = () =>{
if(this.state.isLogged === false){
return <Switch>
<Route exact path ='/registration' component={Register} />
<Route exact path ='/login' component={Login} />
</Switch>
}
}
render() {
return (
<Router>
<div className="Main">
{this.handleClientFirstImpact()}
</div>
</Router>
)
}
}
In "Main" I have two components "Register" and "Login".
How do I make Login page effect the Header's state? is there a React technology or a way to do that?
I want the "UserHeader" to show the User name but I don't know how to effect it's parent's state.
There might be some common component where you will be calling the Main as well as the Header Component. Suppose that component is App Component.
So inside App you have
render() {
return
<div>
<Header />
<Main />
</div>
}
Now what you can do is keep the userName in this App Component's state and this App Component will pass userName to the Component as :-
<Header userName={userName} />
Also pass a function as a prop to the Main Component which will enable the component to set the State of the Parent Component.
<Main setUserName={(newUserName) => this.setState(newUserName)} />
now this setUserName prop should be passed on to the components which are called via Route inside the Main Component. Keeping your example in mind (use render prop instead of component for Route):
export default class Main extends Component {
state ={isLogged : false}
handleClientFirstImpact = () =>{
const { setUserName } =this.props;
if(this.state.isLogged === false){
return
<Switch>
<Route exact path ='/registration'
render={(props) => <Register {...props} setUserName={setUserName} />}
/>
<Route exact path ='/login'
render={(props) => <Login {...props} setUserName={setUserName} />}
/>
</Switch>
}
}
render() {
return (
<Router>
<div className="Main">
{this.handleClientFirstImpact()}
</div>
</Router>
)
}
}
Now you have passed setUserName as a prop to both login and register and you can use this method to set App component's state which will in turn reflect the changes on the Header component.
Although the solution might work for you. I would advise you to simplify the Application layout. Keep the routing functionality in the main app Component. Use a separate layout component to render similar page layouts. It would avoid confusion in the long run.

How to Pass Props to a Component in React

I have 2 page components with their own routes. There is one header component that is to be used in both pages, and it is supposed to change its content depending on what page you're on.
How can I change the content in the header per page?
return (
<main className={theme.main}>
<Header
title={this.state.headerTitle}
boldTitle={this.state.boldTitle}
/>
<div className={theme.content}>
<Switch>
<Route path={'/page1'} component={Page1} />
<Route path={'/page2'} component={Page2} />
</Switch>
</div>
<Footer/>
</main>
);
I am trying to pass props to the header in my Routes page as in the code below.
<Route path={'/page2'} component={Page2} currentTitle={this.state.headerTitle} />
UPDATE
I got it fixed this using the
import { withRouter } from "react-router"; and
export default withRouter(Header);
in Header Component then he listen the routes and I can put the pathname for to do what I need.
Then, I don't need set up state, just listen the routes inside that MyComponent.
Usign: this.props.location
MOVE YOUR HEADER INSIDE Page1 AND Page2.
<Route path={'/page2'} component={() => <Page2 currentTitle={this.state.headerTitle}
/>} />
Then in header use currentTitle to change title.
Put Header into its own component and import it separately into each page component.
Then you can pass props into the header like this, when you call the Header in each page...
import Header from (wherever that component is)
class Page# extends React.component {
other code
render() {
return (
<div>
<Header currentTitle="this.state.headerTitle" />
<rest of code for this page />
</div>
}
or whatever props you need.
I assume that the code you show lies inside the render() method of some React component, so that you have actually a state.
Note that the component attribute in the Route tag expects a value that is a function without arguments. The component to be rendered is the result of the function. But nothing forbids you to make some work before returning a value. I would try something like this
<Route path={'/page1'} component={() => {
this.setState({headerTitle: ...what you want..., boldTitle: ...what you want...})
return <Page1 />
}}/>
If Page1 is just a function, change the "return" line with return Page1()
Hope it helps - Carlos
you can use the redux library as global state
something like this
class Main extends Component{
render(){
return (
<main className={theme.main}>
<Header
title={this.props.headerTitle}
boldTitle={this.state.boldTitle}
/>
<div className={theme.content}>
<Switch>
<Route path={'/page1'} component={Page1}/>
<Route path={'/page2'}component={Page2}
/>
</Switch>
</div>
<Footer/>
</main>
)
}
const mapStateToProps = state =>{
return { headerTitle: state.headerTitle }
}
export default connect(mapStateToProps)(Main)
Page1
class Page1 extends Component{
changeTitle =()=>{
this.props.actions.changeTitle("title from Page1")
}
render(){
return (
<div>
<button onClick={this.changeTitle}>changeTitle</button>
</div>
)
}
const mapDispatchToProps = (dispatch)=> {
return {
actions: bindActionCreators({ changeTitle }, dispatch)
}
}
export default connect(null, mapDispatchToProps)(Page1)
the code it's not functional but this is an Idea how you can use redux for this purpose

Wrapping multiple react-router routes in error boundary

How do you wrap one or more routes in an error boundary component?
I am using React version 16 and have tried wrapping two routes in error boundaries but am experiencing some unexpected behaviour.
I do not get any error messages at all - but one component will, sometimes, not mount.
When switching between two routes using the same child component (form), the parent component will not update or mount at all. The URL in the web browser location bar updates correctly though. (I am using the same child component for add/edit but with different props)
Is this an issue with ErrorBoundary? Do I need to instruct it somehow?
I have read the documentation on reactjs.org but cannot find any information regarding my issue.
Am I missing how the ErrorBoundary is supposed to work?
Happy if you can lead me in the right direction for solving this issue.
See simple code example below.
export const history = createHistory();
const AppRouter = () => (
<Router history={history}>
<div>
<PrivateRoute component={Header} />
<Switch>
<PrivateRoute path="/dashboard" component={DashboardPage} />
<ErrorBoundary key="eb01">
<PrivateRoute path="/category/edit_category/:id" component={EditCategoryPage} exact={true} />
</ErrorBoundary>
<ErrorBoundary key="eb02">
<PrivateRoute path="/create_category" component={AddCategoryPage} exact={true} />
</ErrorBoundary>
</Switch>
</div>
</Router>
);
The Error boundary component
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
const { history } = props;
history.listen((location, action) => {
if (this.state.hasError) {
this.setState({
hasError: false,
});
}
});
}
componentDidCatch(error, errorInfo) {
// Catch errors in any components below and re-render with error message
this.setState({
error: error,
errorInfo: errorInfo
})
// You can also log error messages to an error reporting service here
}
render() {
if (this.state.errorInfo) {
// Error path
return (
<div>
<h2>Something went wrong</h2>
<details style={{ whiteSpace: 'pre-wrap' }}>
{this.state.error && this.state.error.toString()}
<br />
{this.state.errorInfo.componentStack}
</details>
</div>
);
}
// Normally, just render children
return this.props.children;
}
}
export default ErrorBoundary;
Update
I also tried to wrap the component -not the route- in the ErrorBoundary component.
<PrivateRoute path="/category/edit_category/:id"
render={() => (
<ErrorBoundary>
<EditCategoryPage/>
</ErrorBoundary>
)}
exact={true}/>
I now receive an error (the components are correctly imported - I can use them elsewhere in the same file)
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
I had wrapped the React-Router in my own component. That is why my code did not work! doh!
When adding ErrorBoundary in the right place (my own component) everything worked as expected. :)
export const PrivateRoute = ({
isAuthenticated,
component: Component,
useErrorBoundary,
...rest
}) => (
<Route {...rest} component = {(props) => (
isAuthenticated ? (
(useErrorBoundary) ?
<div>
<ErrorBoundary>
<Component {...props} />
</ErrorBoundary>
</div>
:
<div>
<Component {...props} />
</div>
) : (
<Redirect to="/" /> //redirect if not auth
)
)
}/>
);

How to re-render the same component being used in different routes?

I have several routes rendering the same component. Depending on the route I want the component to fetch different data. However since I keep rendering the same component, React doesn't see any changes to the DOM when I click a Link tag (from my nav bar located in the Layout component) to another route rendering that same component. Meaning the component is not re-rendered with the new data. Here are my routes:
class App extends Component {
render() {
return (
<BrowserRouter>
<Provider store={store}>
<Layout>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/fashion" component={PostTypePageContainer} />
<Route exact path="/beauty" component={PostTypePageContainer} />
</Switch>
</Layout>
</Provider>
</BrowserRouter>
);
}
}
export default App;
Here is the PostTypePageContainer component that I want to re-render with the new data each time:
class PostTypePageContainer extends Component {
componentDidMount() {
let route;
switch (this.props.location.pathname) {
case '/fashion':
route = '/fashion';
break;
case '/beauty':
route = '/beauty';
break;
default:
console.log('No data was found');
}
let dataURL = `http://localhost:8888/my-site//wp-json/wp/v2${route}?_embed`;
fetch(dataURL)
.then(res => res.json())
.then(res => {
this.props.dispatch(getData(res));
});
}
render() {
let posts = this.props.postData.map((post, i) => {
return <PostTypePage key={i} props={post} />;
});
return <div>{posts}</div>;
}
}
const mapStateToProps = ({ data }) => ({
postData: data.postData
});
export default connect(mapStateToProps)(PostTypePageContainer);
How do I go about re-rendering that component each time?
This is intended behavior of react-router.
While i suggest you create a HOC to fetch the data from different locations and pass it to the PostTypePageContainer via props, using a key will give you a quick work around that will cause your component to remount.
class App extends Component {
render() {
return (
<BrowserRouter>
<Provider store={store}>
<Layout>
<Switch>
<Route exact path="/" component={Home} />
<Route exact key={uniqueKey} path="/fashion" component={PostTypePageContainer} />
<Route exact key={someOtherUniqueKey} path="/beauty" component={PostTypePageContainer} />
</Switch>
</Layout>
</Provider>
</BrowserRouter>
);
}
}
export default App;
Source: https://github.com/ReactTraining/react-router/issues/1703
I wasn't able to get the <Route key={...} ... /> to work in my case. After trying several different approaches the one that worked for me was using the componentWillReceiveProps function in the reused component. This was getting called each time the component was called from a <Route />
In my code, I did:
componentWillReceiveProps(nextProps, nextContext) {
// When we receive a call with a new tag, update the current
// tag and refresh the content
this.tag = nextProps.match.params.tag;
this.getPostsByTag(this.tag);
}

Resources