尝试查找其长度时未定义字符串
String is undefined when trying to find its length
我在 Ignite 2.0 中使用 redux-form。我正在尝试验证这些值,但每当我尝试查找字符串的长度时,它都会给我 String is undefined
这是我的组件
import React, { Component } from "react";
import { Field, reduxForm } from "redux-form";
import {
Container,
Content,
Text,
Button,
View,
Item,
Input
} from "native-base";
import { StackNavigator } from "react-navigation";
const validate = value => {
const error = {};
error.name = "";
error.password = "";
const { name, password } = value;
if (name === "undefined" || name === "") {
error.name = "Empty";
}
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
console.log("error", error);
};
class SignupForm extends Component {
renderInput({
input,
label,
placeholder,
type,
meta: { touched, error, warning }
}) {
var hasError = false;
if (error !== undefined) {
hasError = true;
}
return (
<Item error={hasError}>
<Input {...input} placeholder={placeholder} type={type} />
{hasError ? <Text>{error}</Text> : <Text />}
</Item>
);
}
render() {
const { handleSubmit } = this.props;
return (
<Container>
<Content>
<Field
name="name"
component={this.renderInput}
label="Name"
placeholder="Name"
type="text"
/>
<Field
name="password"
component={this.renderInput}
placeholder="Password"
label="Password"
type="password"
/>
<Button type="submit" onPress={handleSubmit}>
<Text>Submit</Text>
</Button>
</Content>
</Container>
);
}
}
export default reduxForm({
form: "SignupForm",
validate
})(SignupForm);
password.length 不断抛出错误 TypeError:password is undefined
谁能告诉我我做错了什么或者我错过了什么。
谢谢
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
此处,将 "undefined"
替换为不带双引号的 undefined
,因为 undefined
是原始值。
这样勾选undefined
if (!password && password.length < 8)
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
这里你应该做一个通用检查,只检查!password || password.length < 8
通过这种方式,您也可以处理 null
和 undefined
的情况。
我在 Ignite 2.0 中使用 redux-form。我正在尝试验证这些值,但每当我尝试查找字符串的长度时,它都会给我 String is undefined
这是我的组件
import React, { Component } from "react";
import { Field, reduxForm } from "redux-form";
import {
Container,
Content,
Text,
Button,
View,
Item,
Input
} from "native-base";
import { StackNavigator } from "react-navigation";
const validate = value => {
const error = {};
error.name = "";
error.password = "";
const { name, password } = value;
if (name === "undefined" || name === "") {
error.name = "Empty";
}
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
console.log("error", error);
};
class SignupForm extends Component {
renderInput({
input,
label,
placeholder,
type,
meta: { touched, error, warning }
}) {
var hasError = false;
if (error !== undefined) {
hasError = true;
}
return (
<Item error={hasError}>
<Input {...input} placeholder={placeholder} type={type} />
{hasError ? <Text>{error}</Text> : <Text />}
</Item>
);
}
render() {
const { handleSubmit } = this.props;
return (
<Container>
<Content>
<Field
name="name"
component={this.renderInput}
label="Name"
placeholder="Name"
type="text"
/>
<Field
name="password"
component={this.renderInput}
placeholder="Password"
label="Password"
type="password"
/>
<Button type="submit" onPress={handleSubmit}>
<Text>Submit</Text>
</Button>
</Content>
</Container>
);
}
}
export default reduxForm({
form: "SignupForm",
validate
})(SignupForm);
password.length 不断抛出错误 TypeError:password is undefined 谁能告诉我我做错了什么或者我错过了什么。
谢谢
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
此处,将 "undefined"
替换为不带双引号的 undefined
,因为 undefined
是原始值。
这样勾选undefined
if (!password && password.length < 8)
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
这里你应该做一个通用检查,只检查!password || password.length < 8
通过这种方式,您也可以处理 null
和 undefined
的情况。