0

I have two files. File1 is a class component returning a class using export. File2 is a normal function component. I have a button in File2 I want to use onclick event handler to summon my file1 which I imported in file2. I'm including parts of my code.

import Comment from './commentForm';

<Button type="button" outline onClick= {***I want to call comment here***}>
  Send Feedback
</Button>

Comment is file1 and the button is on file2

3
  • 1
    What is the first file do. Please elaborate your question. It is not clear. Commented Aug 4, 2020 at 15:53
  • Do you want to conditionally render Comment? There are a lot of examples for this, but you can look at this stackoverflow.com/questions/40477245/…. Commented Aug 4, 2020 at 15:59
  • Show both the files else clearly mention what it does Commented Aug 4, 2020 at 16:04

3 Answers 3

1

Your button should be integrated into a wrapping component, do something like this:

import React, { useState } from 'react'
import Comment from './commentForm';

const MyComponent = () => {
  const [displayed, setDisplayed] = useState()

  return (
    <div>
      { displayed && <Comment /> }
      <Button type="button" outline onClick={() => setDisplayed(true)}>
        Send Feedback
      </Button>
    </div>
  )
}

export default MyComponent
Sign up to request clarification or add additional context in comments.

Comments

1

I assume, you want to call a class component method/s in the click handler of another component, lets say you have

  1. Class component : AClassComp in File1.jsx
  2. another component (functional or class) : ParentComp in File2.jsx

you can do the below

// File1.jsx

Class AClassComp extends React.Component{
   constructor(){ ...... }

   someMethod1=()=>{}
   someMethod2=()=>{}
   ...
   render(){.....}
}
export default AClassComp;
 //File2
 import 'AClassComp' from './File1.jsx'

 function ParentComp(){
   const classCompRef = useRef(null);

   const onClickButton= (e)=> {
     // you can access the Class comp methods here 
     // or do what ever you want using the AClassComp instance
     classCompRef.current.someMethod1();
   }

   return (
    <>
    <AClassComp ref={classCompRef}/>
    <Button onClick={onClickButton}
    </>
   )

 }

when you reference a class component it returns its instance .

Comments

0

If you are importing a functional component from another file, you can simply add the variable in the onClick handler. It might be useful to see what 'Comment' actually does?

import Comment from './commentForm';

<Button type="button" outline onClick= {Comment()}>
  Send Feedback
</Button>

EDIT: Assuming Comment looks something like this...

const Comment = () -> {
   return comment
}

export default Comment

1 Comment

I was calling class component methods

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.