1. ホーム
  2. javascript

[解決済み] Expected to return a value at the end of arrow function" の警告を修正するにはどうすればよいですか?

2023-01-06 15:02:08

質問

すべて正常に動作しているのですが、次のような警告が表示されます。 Expected to return a value at the end of arrow function array-callback-return . を使ってみたところ forEach の代わりに map に変更する必要がありますが、その場合は <CommentItem /> は表示されません。どうすればこれを修正できますか?

  return this.props.comments.map((comment) => {
  
      if (comment.hasComments === true) {
      
        return (
          <div key={comment.id}>
          
            <CommentItem className="MainComment"/>

              {this.props.comments.map(commentReply => {
              
                if (commentReply.replyTo === comment.id) { 
                  return (
                    <CommentItem className="SubComment"/>
                 ) // return
                } // if-statement
              }) // map-function
              } // map-function __begin
            
          </div> // comment.id
          
        ) // return

どのように解決するのですか?

警告は、すべてのケースでmap arrow関数の最後に何かを返していないことを示しています。

あなたが達成しようとしているものに対するより良いアプローチは、最初に .filter を使用し、次に .map のような、このような。

this.props.comments
  .filter(commentReply => commentReply.replyTo === comment.id)
  .map((commentReply, idx) => <CommentItem key={idx} className="SubComment"/>);