0

We have 2 components. one is Test1 and the second one is Test2. My question is how we can pass data from component Test1 to Test2(they are separated components.) onClick event. : for e.g a function like this:

  const ClickHandler =()=>{
        //pass data to Test2
      
  }
1
  • 1
    Data is generally passed via props to children components, but there are other methods for storing data and passing it around. You've not provided enough context to really provide a useful answer. Can you update your question to include a Minimal, Complete, and Reproducible Code Example? Commented Jun 11, 2021 at 8:54

2 Answers 2

1

I think there will be two methods to do this.

  1. Be a child of the same parent and share props.
  import {useState} from "React"
   
  const TestOneComponent = ({value}) => (
    <span>{value.toString()}<span>
  )

  const TestTwoComponent = ({value, onClick}) => (
    <span>{value.toString()}</span>
    <button onClick={onClick}>Increase Value</button>
  )

  const ParentComponent = () => {
    const [value, setValue] = useState(0)

    const onClick = () => {
      setValue(value + 1)
    }

    return (
      <>
        <TestOneComponent value={value}>
        <TestTwoComponent value={value} onClick={onClick}>
      </>
    )
  }
  1. Manage store and pass props to those two components.
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain method 2, please?
It is the same as the first method in concept. Share props. But just use global store.
0

Either you can use props to pass data in to the other component and render it when the onClick event is triggered or

you can navigate to a new page by onClick event and pass data into the header/route and access that data in the new page's component

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.