1

I'm creating a react component of subcomponents with:

const Miniviews = ({reducedArry}) => {
  Object.keys(reducedArry).map((applicationId) => {
    return (
      <div id={applicationId}>
        {reducedArry[applicationId].map(miniview => (
          <Miniview
            key={miniview.id}
            id={miniview.id}
            title={miniview.name}
            handleOpenButton={this.handleMiniviewOpenView}
            />)
        )}
      </div>
    )
  })
};
console.log(Miniviews);

and displaying it in the render method with

{ this.state.showViews      && <Miniviews reducedArry={reducedArry} /> }

however, the value of Miniviews is actually not the returned objects from the function, but the object itself. The console.log outputs:

function Miniviews(_ref) {
        var reducedArry = _ref.reducedArry;

        Object.keys(reducedArry).map(function (applicationId) {
          return _react2.default.createElement(
           …

How can I make it return what I expect, which is:

<div id="1">
<Miniview />
<Miniview />
</div>
<div id="2">
<Miniview / >
</div>
1
  • 2
    For questions like this, it's really useful to put a runnable minimal reproducible example using Stack Snippets (the [<>] toolbar button). Stack Snippets support React, including JSX; here's how to do one. That way, people helping you can see the problem actually happen, and can show you the fix working in their answers. Commented Mar 9, 2017 at 15:13

2 Answers 2

2

Two issues:

  1. "the value of Miniviews is actually not the returned objects from the function" That's right, you're defining Miniviews as a function (an arrow function), and not calling it. To call it, you'd do Miniviews(relevantArgumentHere) after.

  2. You're using a verbose arrow function, which means you need to use an explicit return. If you use a concise arrow function (without { after =>), the return is implicit.

Here's an example of a concise arrow function:

const f = () => "foo";

and the equivalent verbose:

const f = () => { return "foo"; };

So either concise:

const Miniviews = ({reducedArry}) => // Note no { here
  Object.keys(reducedArry).map((applicationId) => {
    return (
      <div id={applicationId}>
        {reducedArry[applicationId].map(miniview => (
          <Miniview
            key={miniview.id}
            id={miniview.id}
            title={miniview.name}
            handleOpenButton={this.handleMiniviewOpenView}
            />)
        )}
      </div>
    )
  })
; // And no } before the ; here
console.log(Miniviews(someArgumentHere));
//                   ^^^^^^^^^^^^^^^^^^----- calling the function

or verbose with return:

const Miniviews = ({reducedArry}) => {
  return Object.keys(reducedArry).map((applicationId) => {
// ^^-- note this return
    return (
      <div id={applicationId}>
        {reducedArry[applicationId].map(miniview => (
          <Miniview
            key={miniview.id}
            id={miniview.id}
            title={miniview.name}
            handleOpenButton={this.handleMiniviewOpenView}
            />)
          )}
      </div>
    )
  })
};
console.log(Miniviews(someArgumentHere));
//                   ^^^^^^^^^^^^^^^^^^----- calling the function
Sign up to request clarification or add additional context in comments.

4 Comments

It's also the case that the function isn't being called, right?
@Pointy: Yeah, I just added #1 above, I missed the comment in the question about the result of the console.log initially.
Using the return gives me a: Uncaught TypeError: Cannot convert undefined or null to object
@Wayneio: It shouldn't if you put the return where it is above. (Side note: I removed the wrong } in the concise form -- fixed now -- but if you added the return and stayed with the verbose form that wouldn't be the problem.)
1

You never return anything from Miniviews function.

Try this:

const Miniviews = ({reducedArry}) => {
 return Object.keys(reducedArry).map((applicationId) => {
   return (
     <div id={applicationId}>
       {reducedArry[applicationId].map(miniview => (
         <Miniview
           key={miniview.id}
           id={miniview.id}
           title={miniview.name}
           handleOpenButton={this.handleMiniviewOpenView}
           />)
         )}
       </div>
     )
   })
 };

1 Comment

The function (Miniviews) isn't called, so the value of Miniviews is still just a function reference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.