函数参数中传入组件作为参数,编译失败,急急急!!!



  • import Taro, { Component } from '@tarojs/taro'
    import { View, Text } from '@tarojs/components'
    import './test.scss'
    
    export default class Test extends Component {
    
      constructor() {
        super(...arguments)
        this.state = {
          list: []
        }
      }
    
      componentWillMount () { }
    
      componentDidMount () {
        this.addItem({
          info: <View>hello react</View>
        })
       }
    
      componentWillUnmount () { }
    
      componentDidShow () { }
    
      componentDidHide () { }
    
      config = {
        navigationBarTitleText: '首页'
      }
    
      addItem = (data) => {
        const { list } = this.state
        list.push(data)
        this.setState({
          list: [...list]
        })
      }
    
      render () {
        const { list } = this.state
        return (
          <View className='test'>
            {
              list.map((val,index) => {
                <View key={`item${index}`}>
                  {val.info}
                </View>
              })
            }
          </View>
        )
      }
    }
    
    

    Warning: React version not specified in eslint-plugin-react settings. See https://github.com/yannickcr/eslint-plugin-react#configuration .
    编译 发现入口 src/app.jsx
    ⠙ 正在编译...Error: 没有找到返回的 JSX 元素,你是不是忘记 return 了?

    14 | componentWillMount () { }
    15 |

    16 | componentDidMount () {
    | ^^^^^^^^^^^^^^^^^^^^^^^
    17 | this.addItem(<View>hello react</View>)
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    18 | }
    | ^^^^
    19 |
    20 | componentWillUnmount () { }
    21 |
    at Object.codeFrameError (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/utils.ts:405:10)
    at RenderParser.setOutputTemplate (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/render.ts:2216:13)
    at new RenderParser (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/render.ts:1661:10)
    at renderJSX.forEach (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/class.ts:1125:11)
    at Map.forEach (<anonymous>)
    at Transformer.parseRender (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/class.ts:1123:22)
    at Transformer.compile (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/class.ts:1159:10)
    at new Transformer (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/class.ts:145:10)
    at Function.transform [as default] (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/transformer-wx/src/index.ts:885:12)
    at Object.wxTransformerLoader (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/@tarojs/mini-runner/dist/loaders/wxTransformerLoader.js:69:61)
    at LOADER_EXECUTION (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/loader-runner/lib/LoaderRunner.js:119:14)
    at runSyncOrAsync (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/loader-runner/lib/LoaderRunner.js:120:4)
    at iterateNormalLoaders (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
    at Array.<anonymous> (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
    at Storage.finished (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
    at provider (/Users/chenhaoxin881/Develop/myTaroApp/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9)
    🙅 编译失败.

    ./src/pages/test/test.jsx
    Module Error (from ./node_modules/@tarojs/mini-runner/dist/loaders/wxTransformerLoader.js):
    没有找到返回的 JSX 元素,你是不是忘记 return 了?

    14 | componentWillMount () { }
    15 |

    16 | componentDidMount () {
    | ^^^^^^^^^^^^^^^^^^^^^^^
    17 | this.addItem(<View>hello react</View>)
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    18 | }
    | ^^^^
    19 |
    20 | componentWillUnmount () { }
    21 |

    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! myTaroApp@1.0.0 build:weapp: taro build --type weapp
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the myTaroApp@1.0.0 build:weapp script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

    npm ERR! A complete log of this run can be found in:
    npm ERR! /Users/chenhaoxin881/.npm/_logs/2020-07-09T07_17_11_607Z-debug.log



  • @sj18578607245 求个可复现的demo提交到issue


登录后回复