Reactjs 失败的道具类型:道具'message'在'Description'中标记为必需,但其值为'undefined'`
在我的index.jsx主页中有以下代码Reactjs 失败的道具类型:道具'message'在'Description'中标记为必需,但其值为'undefined'`,reactjs,jsx,Reactjs,Jsx,在我的index.jsx主页中有以下代码 toggleDescription(product, success = true) { if (success) { this.descriptionText = t('products/product_description', { name: item.product, }); this.setState({ open: !open failure: !success
toggleDescription(product, success = true) {
if (success) {
this.descriptionText = t('products/product_description', {
name: item.product,
});
this.setState({
open: !open
failure: !success
});
}
---some code here---
return (
<div>
</Description
isOpen={this.state.open}
onRequestClose={this.toggleDescription}
message={this.descriptionText}
isError={failure}
>
<div> --delete and close buttons here-- </div>
</Description>
toggleDescription(产品,成功=true){
如果(成功){
this.descriptionText=t('产品/产品描述'{
名称:item.product,
});
这是我的国家({
开门!开门
失败!成功
});
}
---这里有一些代码---
返回(
--此处的删除和关闭按钮--
我有descriptionText的翻译。我正在切换descriptionText组件。当我第一次加载应用程序时,我在标题中得到了错误,因为描述文本未定义。我如何解决此问题?第一个选项是,您可以将
此.descriptionText
设置为默认值,如空字符串或构造函数上的某个内容>
constructor(props) {
super(props);
this.descriptionText = 'some default value or empty string';
}
或者你可以像下面那样使用它
message={this.descriptionText || 'some default value or empty string'}
第一个选项是,您可以将this.descriptionText
设置为默认值,如空字符串或构造函数上的某个内容
constructor(props) {
super(props);
this.descriptionText = 'some default value or empty string';
}
或者你可以像下面那样使用它
message={this.descriptionText || 'some default value or empty string'}
您确实不应该将值直接保存到组件。除非这些值由状态管理,否则不能保证子级可以重新呈现
你应该改变这一点
getInitialState() {
return {
descriptionText: 'default text'
}
},
toggleDescription(product, success = true) {
if (success) {
this.setState({
descriptionText: t('products/product_description', {
name: item.product,
}),
open: !open,
failure: !success
});
},
render() {
return (
<div>
</Description
isOpen={this.state.open}
onRequestClose={this.state.toggleDescription}
message={this.state.descriptionText}
isError={failure}
>
<div> --delete and close buttons here-- </div>
</Description>
getInitialState(){
返回{
descriptionText:“默认文本”
}
},
toggleDescription(产品,成功=真){
如果(成功){
这是我的国家({
descriptionContext:t(“产品/产品描述”{
名称:item.product,
}),
开门!开门,
失败!成功
});
},
render(){
返回(
--此处的删除和关闭按钮--
您确实不应该将值直接保存到组件。除非值由状态管理,否则无法保证子级重新呈现
你应该改变这一点
getInitialState() {
return {
descriptionText: 'default text'
}
},
toggleDescription(product, success = true) {
if (success) {
this.setState({
descriptionText: t('products/product_description', {
name: item.product,
}),
open: !open,
failure: !success
});
},
render() {
return (
<div>
</Description
isOpen={this.state.open}
onRequestClose={this.state.toggleDescription}
message={this.state.descriptionText}
isError={failure}
>
<div> --delete and close buttons here-- </div>
</Description>
getInitialState(){
返回{
descriptionText:“默认文本”
}
},
toggleDescription(产品,成功=真){
如果(成功){
这是我的国家({
descriptionContext:t(“产品/产品描述”{
名称:item.product,
}),
开门!开门,
失败!成功
});
},
render(){
返回(
--此处的删除和关闭按钮--