React Native JSX:设置状态导致应用程序崩溃
React Native JSX: Setting state causes app to crash
我正在使用 React Native 来构建我的应用程序。下面是我用来显示 'tags' 列表的代码。所以代码是用来隐藏除前2个标签外的所有标签,会出现一个'load more' link。单击加载更多 link 应该会显示其余标签。但是代码在我身上崩溃了。
this.state = {
visibleCount: 2,
};
<TextLink onPress={() => {
this.setState({visibleCount: mealTags.length});
}}
</TextLink>
我正在使用更改状态来显示标签。谁能告诉我出了什么问题以及如何更新它?
export function MealTagsSection(props: MealTagsProps) {
let {mealTags} = props;
let loadMoreLink;
if (mealTags.length > 2) {
loadMoreLink = (
//THIS CAUSES THE APP TO CRASH
<TextLink onPress={() => {
this.setState({visibleCount: mealTags.length});
}}
>
load more...
</TextLink>
);
} else {
loadMoreLink = null;
}
this.state = {
visibleCount: 2,
};
return (
<View style={styles.mealTagsContainer}>
{
mealTags.slice(0, this.state.visibleCount).map((mealTag) => {
let tagStyle = '';
if (mealTag.category === 1) {
tagStyle = styles.tag_healthy;
} else {
tagStyle = styles.tag_improve;
}
return (
<View style={tagStyle}>
<Text style={styles.tagText}>{mealTag.description}</Text>
</View>
);
})
}
{loadMoreLink}
</View>
);
}
我得到的错误是:
*** 由于未捕获的异常 'RCTFatalException:未处理的 JS 异常:t.setState 不是函数而终止应用程序。 (在 't.setState({visibleCount:n.length})' 中,'t.setState' 未定义)', reason: 'Unhandled JS Exception: t.setState is not a function. (在't.setState({visi..., stack: onPress@439:2034
您的 MealTagsSection
是功能组件。 React 功能组件不必包含本地状态。如果想拥有本地状态,则应将其设为 class 组件。
export class MealTagsSection extends Component {
constructor() {
super();
this.state = { visibleCount: 2 };
}
render() {
let { mealTags } = this.props;
let loadMoreLink;
if (mealTags.length > 2) {
loadMoreLink =
(
<TextLink
onPress={() => {
this.setState({ visibleCount: mealTags.length });
}}
>
load more...
</TextLink>
);
} else {
loadMoreLink = null;
}
return (
<View style={styles.mealTagsContainer}>
{mealTags.slice(0, this.state.visibleCount).map(mealTag => {
let tagStyle = "";
if (mealTag.category === 1) {
tagStyle = styles.tag_healthy;
} else {
tagStyle = styles.tag_improve;
}
return (
<View style={tagStyle}>
<Text style={styles.tagText}>{mealTag.description}</Text>
</View>
);
})}
{loadMoreLink}
</View>
);
}
}
我正在使用 React Native 来构建我的应用程序。下面是我用来显示 'tags' 列表的代码。所以代码是用来隐藏除前2个标签外的所有标签,会出现一个'load more' link。单击加载更多 link 应该会显示其余标签。但是代码在我身上崩溃了。
this.state = {
visibleCount: 2,
};
<TextLink onPress={() => {
this.setState({visibleCount: mealTags.length});
}}
</TextLink>
我正在使用更改状态来显示标签。谁能告诉我出了什么问题以及如何更新它?
export function MealTagsSection(props: MealTagsProps) {
let {mealTags} = props;
let loadMoreLink;
if (mealTags.length > 2) {
loadMoreLink = (
//THIS CAUSES THE APP TO CRASH
<TextLink onPress={() => {
this.setState({visibleCount: mealTags.length});
}}
>
load more...
</TextLink>
);
} else {
loadMoreLink = null;
}
this.state = {
visibleCount: 2,
};
return (
<View style={styles.mealTagsContainer}>
{
mealTags.slice(0, this.state.visibleCount).map((mealTag) => {
let tagStyle = '';
if (mealTag.category === 1) {
tagStyle = styles.tag_healthy;
} else {
tagStyle = styles.tag_improve;
}
return (
<View style={tagStyle}>
<Text style={styles.tagText}>{mealTag.description}</Text>
</View>
);
})
}
{loadMoreLink}
</View>
);
}
我得到的错误是: *** 由于未捕获的异常 'RCTFatalException:未处理的 JS 异常:t.setState 不是函数而终止应用程序。 (在 't.setState({visibleCount:n.length})' 中,'t.setState' 未定义)', reason: 'Unhandled JS Exception: t.setState is not a function. (在't.setState({visi..., stack: onPress@439:2034
您的 MealTagsSection
是功能组件。 React 功能组件不必包含本地状态。如果想拥有本地状态,则应将其设为 class 组件。
export class MealTagsSection extends Component {
constructor() {
super();
this.state = { visibleCount: 2 };
}
render() {
let { mealTags } = this.props;
let loadMoreLink;
if (mealTags.length > 2) {
loadMoreLink =
(
<TextLink
onPress={() => {
this.setState({ visibleCount: mealTags.length });
}}
>
load more...
</TextLink>
);
} else {
loadMoreLink = null;
}
return (
<View style={styles.mealTagsContainer}>
{mealTags.slice(0, this.state.visibleCount).map(mealTag => {
let tagStyle = "";
if (mealTag.category === 1) {
tagStyle = styles.tag_healthy;
} else {
tagStyle = styles.tag_improve;
}
return (
<View style={tagStyle}>
<Text style={styles.tagText}>{mealTag.description}</Text>
</View>
);
})}
{loadMoreLink}
</View>
);
}
}