0

I have to call a functional component from another functional component So how can I call child functional component from a functional component in react js.

import React from "react";
import TestFunctional from "./TestFucntional";

const TestListing = props => {
  const { classes, theme } = props;

  const handleClickTestOpen = () => {
    return <TestFunctional />;
  };

  return (
    <div>
      <EditIcon
        className={classes.icon}
        onClick={handleClickTestOpen}
      />    
    </div>
  );
};

export default TestListing;

I am trying to call or render TestFucntional component on EditIcon clicked but it is not called. So How can I call component?

Thanks.

4
  • What do you mean by calling a component. Do you mean a function within the functional component Commented Jul 17, 2019 at 5:56
  • @ShubhamKhatri. how can you call a function within a functional component from a parent component? Commented Sep 10, 2020 at 21:05
  • @Elaine You can make use of useImperativeHandle and ref to expose the child component function to be used in parent. However, a better idea in most cases is to them move the function to parent and pass it on as props to child Commented Sep 11, 2020 at 4:14
  • @ShubhamKhatri. thanks for your quick response. my use case is i have a functional component with d3 tree. i want to create buttons to expand and collapse all. if i put it in the functional component the buttons are recreated too many times. So I created the buttons in the parent component render. how do I call the expand and collapse function in the child functional component from the parent? Commented Sep 11, 2020 at 15:33

2 Answers 2

1

You just use it in your jsx as a component as usual. You can see here

const ItemComponent = ({item}) => (
<li>{item.name}</li>)

const Component1 = ({list}) => (
<div>
  MainComponent
  <ul>
    {list && list.map(item =><ItemComponent item={item} key={item.id}/>)}
  </ul>
</div>)

const list = [{ id: 1, name: 'aaa'}, { id: 2, name: 'bbb'}]

ReactDOM.render(
  <Component1 list={list}/>
  , document.querySelector('.container')
);

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

8 Comments

Thanks It is working fine to call one component from another component. but If I have to call functional component on button click then How can I call.
Do you mean to call a function from the parent component?
No, I have to call or render a child component from a parent component in functional component.
import React from "react"; import EditIcon from "@material-ui/icons/Edit"; import TestFunctional from "./TestFucntional"; const TestListing = props => { function handleClickKanbanOpen(testid, testname) { return <TestFunctional />; } return ( <div> <EditIcon onClick={event => { handleClickKanbanOpen("1", "test"); }} /> </div> ); }; export default TestListing; I have to call TestFunctional component on edit icon click event.
add it as code in your question, it is easier to understand
|
0

From the above conversation, I guess you want conditional rendering, i.e. after any event you want to render the child component. To do so in the parent component, it should maintain a state. If you want to use functional parent component, you can use hooks. Or you can use some prop for the conditional rendering as well. Please provide a code snippet.

This is for reference: https://reactjs.org/docs/conditional-rendering.html

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.