-1

I am trying to use state variable from a client component which accepts certain form values from the user in component A and then I need to use those values in component B which is a server component and is mainly responsible for querying a .XML file with the form values.

However, let's say if I have two files in the app directory called A.tsx and B.tsx. How do I access the 'aValues' in B.tsx?

A.tsx:

'use client'

import { useState } from "react"

const A = () => {
    const [aValues, setAValues] = useState({
        name: '',
        phone: ''
      });

  return (
    <div>A is a Client component</div>
  )
}

export default A

B.tsx:

const B = () => {
  
  return (
    <div>B is a Server component and wants to display the aValues</div>
  )
}

export default B
  1. I tried adding a Link tag and importing it in the b using useSearchParams but then I need to change component B to client as well.

A.tsx:

 <Link href={{ pathname: 'b', query: {aValues: JSON.stringify(aValues)}}}> B </Link>

B.tsx:

   'use client'
   import {useSearchParams} from 'next/navigation'
   
   const B = () => {
       const searchParams = useSearchParams()
       const aValues = searchParams.get('aValues')
       const result = JSON.parse(aValues)
      
       <div>{result.name}</div>
   }
   
   export default B
  1. I also tried creating a global state with Redux and use it but felt it to be rather too complicated and a bit of an overkill for the task.

  2. Additionally, I also tried creating a new C.tsx and importing <A/> and <B/> just to pass the aValues as children in B but still couldn't do it.

C.tsx

import A from 'A'
import B from 'B'

const C = () => {
  return (
    <div>
        <A>
        <B name={aValues.name} phone={aValues.phone}/>
        </A>
    </div>
  )
}

export default C
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Ishaan_B
  • 9
  • 6
  • use `useContext` hook – Kannu Mandora Jul 20 '23 at 15:19
  • 1
    What exactly are you trying to do here? You wan't to pass some of A's "state" to B? These would need to be passed as props from A to B if A is the parent of B. If A and B are siblings then you'll need to lift A's state higher in the ReactTree so it can be passed down as props. Does this makes sense? If not, then we'll need better context what you are trying to accomplish, e.g. a better [mcve]. – Drew Reese Jul 20 '23 at 15:42
  • @KannuMandora, thanks. I will try and see if I am able to get the data. – Ishaan_B Jul 20 '23 at 16:06
  • Hey @DrewReese, thanks for reaching out. Component A and B are in siblings. I couldn't share my actual project files, hence I created a hypothetical example with the core issue. However to better clarify the problem in the project, Component A has a form to accept values and these values are stored in a state variable. Component B is where I need to use those state values from Component A. Does that make sense? – Ishaan_B Jul 20 '23 at 16:10
  • Right, so *somewhere* you need A to pass the data up the ReactTree (*via callback that was passed down as props or Context*), could be state in a common ancestor component, so it can be passed back down the ReactTree as props or React Context to B. Does this make sense to you? – Drew Reese Jul 20 '23 at 16:13
  • @DrewReese I see, I am not too familiar with passing data up through the common ancestor just to pass it back down but thanks I would try it. – Ishaan_B Jul 20 '23 at 16:45
  • Take a look at the [Lifting State Up](https://legacy.reactjs.org/docs/lifting-state-up.html) docs, they may help clarify the React concept. – Drew Reese Jul 20 '23 at 16:58
  • Will do, thanks! – Ishaan_B Jul 20 '23 at 17:05

1 Answers1

0

I am not sure what are you trying to do but if you just want to display the value from clientComponent A to B why don't you just import the client component inside B ?

However if you want to play with useState in B than B component will need to be client anyway

'use client'

import { useState } from "react"

const ComponentA = () => {
    const [aValues, setAValues] = useState({
        name: '',
        phone: ''
      });

  return (
    <div>{aValues.name}</div>  // A client component   
  ) }

export default ComponentA

B server component

import ComponentA from "componentA path" //the right path from your componentA

const ComponentB = () => {
  
  return (
    <ComponentA />
  )
}

export default ComponentB
ShueiYang
  • 648
  • 1
  • 3
  • 7
  • Hey, thanks for sharing your solution. I am trying to access the state variable named 'aValues' in Server component 'B' from the Client Component 'A'. If I try what you proposed some of my imports in component B break and throw an error saying Module not found: can't resolve the module ' '. – Ishaan_B Jul 20 '23 at 15:26
  • Did you correctly import your client component into B ? you have to check your tree structure I just give an example you have to fix your path. – ShueiYang Jul 20 '23 at 15:31
  • Apologies for the confusion, the above question is just an example whereas in my actual project I have Component A which has a form to accept values and these values are stored in a state variable. Component B is where I need to use those state values from Component A. – Ishaan_B Jul 20 '23 at 16:04