简体   繁体   English

如何在const方法中返回

[英]How to return in a const method

Hi I am new to react so sorry for the basic question. 嗨,我是新来的,对基本问题感到抱歉。

I am trying to define a method in a const 我试图在const中定义一个方法

const Age = t.refinement(t.Number, (n) => return n >= 18);

However, the linter doesn't like that i have a return keyword in the method. 但是,linter不喜欢我在方法中有一个return关键字。 Here is the class 这是班级

import React, { Component } from 'react'
import { postFeedback } from 'Services/Config'
import { render } from 'react-dom';
import t from 'tcomb-form';


const FormSchema = t.struct({
    name: t.String,         // a required string
    age: t.Number, // an optional number
    rememberMe: t.Boolean   // a boolean
})

const Age = t.refinement(t.Number, (n) => return n >= 18);

export class Form extends Component {

onSubmit = (evt) => {
    evt.preventDefault()
    const value = this.refs.form.getValue()
    console.log(value)
    console.log("validation ->   " + this.refs.form.validate())
    if (value) {
        console.log(value)
    }
}

render() {
    return (
        <form onSubmit={this.onSubmit}>
            <t.form.Form ref="form" type={FormSchema} />
            <div className="form-group">
                <button type="submit" className="btn btn-primary">Save</button>
            </div>
        </form>
    )
}

}


export default Form

This is an anonymous ES2015 Arrow function , this is not related to the const key word. 这是一个匿名的ES2015箭头函数 ,这与const关键字无关。
Arrow functions returns implicitly or explicitly, depends if you are using a function body block {} . 箭头函数隐式或显式返回,取决于您是否使用函数体块{}

Either do this (explicit return): 这样做(显式返回):

const Age = t.refinement(t.Number, (n) => {return n >= 18});

Or do this (implicit return): 或者这样做(隐式返回):

const Age = t.refinement(t.Number, (n) => n >= 18);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM