2

I have a function that returns this jsx mixed with some code like this:

showMedia = () => {

  return (
            <div>
                {
                    <div id="mainContent">
                        {props.files.map((file) =>
                            const ext = fileExtension(file.name),
                            const icon = fileIcon(ext),
                            const isImg = isImage(ext),
                            <div key={file.id}>
                                <DisplayResults
                                    fileId={file.id}
                                    downloadName={file.downloadName}
                                    fileTitle={file.title}
                                    isImg={isImg}
                                    icon={icon}
                                    ext={ext}
                                />
                            </div>
                        )}
                    </div>
                }
            </div>
         );
}

In my editor, it's saying that it is expecting an '{' for the const variables I am setting after .map

Am I not using the correct syntax?

Thanks!

2 Answers 2

2

Since you don't have a function body {} for your arrow function, it is expecting an implicitly returned expression.

Give it a function body and declare your variables and return the JSX instead:

showMedia = () => {
  return (
    <div>
      {
        <div id="mainContent">
          {props.files.map(file => {
            const ext = fileExtension(file.name);
            const icon = fileIcon(ext);
            const isImg = isImage(ext);
            return (
              <div key={file.id}>
                <DisplayResults
                  fileId={file.id}
                  downloadName={file.downloadName}
                  fileTitle={file.title}
                  isImg={isImg}
                  icon={icon}
                  ext={ext}
                />
              </div>
            );
          })}
        </div>
      }
    </div>
  );
};
Sign up to request clarification or add additional context in comments.

Comments

2

You have to add "return" in map function because map always expect to return something, so your problem can be solved by adding this

{props.files.map(file => {
  return (
    <div key={file.id}>
      {/* something */}
    </div>
  );
})}

Hope this help

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.