DEV Community

Gaurav Joshi
Gaurav Joshi

Posted on

how to use data of useParams in createContext hook in functional component react js

i want to pass data of useParams() inside the createContext() so that i can use data in different components

login component:-

[import React,{createContext} from 'react';
import {useParams} from 'react-router-dom';

export const Context = createContext(companyName);

function Login(){
const {companyName} = useParams();

return(

//somedata//<div?

)
}

export default Login]
Note:- in companyName i have data {name = "hee"}

another component:-

[import React,{useContext} from 'react';
import { Context } from './Login';
function Component(){
const aa = useContext(Context)
console.log("context",aa)
}]
in this component i get context undefined

i try this also

[ import React,{createContext} from 'react';
import {useParams} from 'react-router-dom';

export const Context = createContext();

function Login(){
const {companyName} =  useParams();  
return(
  <Context.Provider value={companyName}>
    <StateMaster/>
  </Context.Provider>

    <div>//somedata//<div?    
)
} ]

export default Login

this time app crashed, login component not working (render), i just want to pass data of useParams into createContext. please help to accomplish this

Top comments (0)