0

In my return, I call a function called renderRuns as follows.

return (
    <div>
        {renderRuns()}
    </div>
)

My renderRuns() looks like

const renderRuns = (type) => {
    const renderNested = (run) => {
        return (
            [1,2,3].map((item, idx) => {
                return <div>{run + item}</div>
            })

        )
    }

    [1,2,3].map((run, idx) => {
          renderNested(run)
    })
}

Basically trying to render a nested component, but not getting any output without an error.

Any thoughts?

3
  • 1
    return [1,2,3].map((... ? And one inside - return renderNested(run) Commented Apr 27, 2020 at 20:42
  • can you share the error? Commented Apr 27, 2020 at 20:43
  • @GokuAfrica not getting any error, but it just doesn't render anything Commented Apr 27, 2020 at 20:45

3 Answers 3

4

Your renderRuns function does not return anything, that's why nothing appears. Simply add a return keyword.

const renderRuns = (type) => {
    const renderNested = (run) => {
        return (
            [1,2,3].map((item, idx) => {
                return <div>{run + item}</div>
            })

        )
    }

    return [1,2,3].map((run, idx) => renderNested(run));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Does it have to follow the same pattern as you specified?

What do you think of this solution?

const renderRuns = (type) => {
  return (
   [1,2,3].map((item, idx) => {
     return <div>{run + item}</div>
   })
}

1 Comment

I actually modified my code in my question to simplify my question. I just wanted to render items in a nested way.
0

you're pretty close, try this

import React from "react";
import "./styles.css";

export default function App() {
  const renderRuns = type => {
    const renderNested = run => {
      return [1, 2, 3].map((item, idx) => {
        return <div>{run + item}</div>;
      });
    };
    // return each call to renderedNested
    return [1, 2, 3].map((run, idx) => renderNested(run));
  };
  return <div>{renderRuns()}</div>;
}

the above works but it may be more useful to return a component instead of calling a function like this:

import React from "react";
import "./styles.css";

export default function App() {
  // receive props around from outterComponent
  const InnerComponent = ({ run }) => {
    return [1, 2, 3].map(item => <div>{run + item}</div>);
  };


  const OutterComponent = props => {
    // have can pass props around
    return [1, 2, 3].map(run => <InnerComponent {...{ run, ...props }} />);
  };

  return (
    <div>
      {/* easily pass props and stay with a known rendering pattern */}
      <OutterComponent />
    </div>
  );
}

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.