0

I have a variable in my child component that holds the data of its height. I need to pass it through its parent and into the parent's parent. I am able to pass it to the parent, but don't understand why passing it to the parent's parent isn't working. The error I am getting is:

Unhandled Runtime Error
TypeError: props.func is not a function

Source
components\layout.js (22:10) @ Object.pull_data [as func]

  20 |  const navHeight = data;
  21 |   console.log(navHeight); 
> 22 |   props.func(navHeight);

  24 | }

Child component:

export default function Navbarmain(props) {

  const ref = useRef();
  useEffect(() => {
    const navHeight = ref.current?.clientHeight;
    props.func(navHeight);
  }, []);


  return (
    <div ref={ref}>

Child's parent:

export default function Layout({ children, home }, props) {

  const pull_data = (data) => {

   const navHeight = data;
    console.log(navHeight); // Correctly outputs Height of child component
    props.func(navHeight);  // Attempt to do a repeat of what I just did

  }

  return (
    <div className={styles.layoutWrap}>
      <Navbarmain func={pull_data}></Navbarmain>

Parent's parent:

export default function Home({ allPostsData }) {
      const pull_data = (data) => {
    
        const navHeight = data;
         console.log(navHeight); 
    
       }
     
    
    
      return (
        <div>
          <Layout home func={pull_data}>
3
  • 1
    Why are you passing a function around in props rather than just passing your variable? Commented May 3, 2022 at 20:34
  • Not sure, how do I do that? Just picked up react this week Commented May 3, 2022 at 20:40
  • 1
    You're trying to use 2 parameters in Layout, which is wrong. The first parameter is props, if you want to use children, home and the rest should be called props you should do the following: {children, home, ...props} and then use it as props.func(navHeight) or destructure also the func as: {children, home, func} and then do func(navHeight) Commented May 3, 2022 at 20:51

1 Answer 1

1

Problem is in the Child's parent' s component props. You are mixing props inside curly braces with props ({ children, home }, props). If you refactor in this way it will work without problem

https://codesandbox.io/embed/fervent-cohen-td6sig?fontsize=14&hidenavigation=1&theme=dark

export default function Layout({ children, home, func }) {

  const pull_data = (data) => {

   const navHeight = data;
    console.log(navHeight); // Correctly outputs Height of child component
    func(navHeight);  // Attempt to do a repeat of what I just did

  }

  return (
    <div className={styles.layoutWrap}>
      <Navbarmain func={pull_data}></Navbarmain>
Sign up to request clarification or add additional context in comments.

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.