ReactJS 中没有渲染方法的渲染组件

Rendering component with no render method in ReactJS

我有一个关于 React 和 Meteor 的问题。在 Meteor App 中,我使用 React Router 进行电子邮件验证 Accounts.verifyEmail ,这里我在将参数传递给组件时遇到问题。在组件中,我只想执行函数而不渲染任何东西。但是我得到一个错误 Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null. 可以解释一下如何解决这个问题吗?

routes.js

import { Meteor } from 'meteor/meteor';
import VerifyEmail from '../imports/ui/verifyEmail'

const routes = () => (
  <Router>
    <Switch>
        <Route name="confirm-account" path="/confirm-account/:token" render = {(props)=><VerifyEmail {...props}/>}/>                 
    </Switch>
  </Router>    
)

export default routes

../imports/ui/verifyEmail.js

const VerifyEmail = ({match:{params}}) => {
  let token = params.token
  Accounts.verifyEmail(token, function(error){
    if(error){
        console.log(error)
    } else {
        console.log('Works')
    }
  })
};

导出默认 VerifyEmail

组件需要有渲染功能。如果 VerifyEmail 不是应该呈现的东西,而只是运行逻辑的东西,也许它应该只是一个函数而不是一个组件。

这实际上是否应该是一个组件是值得商榷的,因为它实际上并没有渲染任何东西,但如果你确实需要它作为一个组件,你收到的错误消息实际上告诉你如何才能解决这个问题。

Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

您可以从组件中 return null 以不呈现任何内容。方法如下。

const VerifyEmail = ({match:{params}}) => {
  let token = params.token
  Accounts.verifyEmail(token, function(error){
    if(error){
        console.log(error)
    } else {
        console.log('Works')
    }
  })

  return null;
};