简体   繁体   English

REACT-Native条件道具投掷错误

[英]REACT-Native conditional props throwing error

I'm fairly new REACT/REACT-Native developer.我是相当新的 REACT/REACT-Native 开发人员。 I'm building a React-Native app and I have a form that is used to create a new budget or edit a budget.我正在构建一个 React-Native 应用程序,并且我有一个用于创建新预算或编辑预算的表单。 I started out simple and had two separate components for each create and edit functions.我一开始很简单,每个创建和编辑功能都有两个独立的组件。 I got all my functionality working so I started refactoring my code to make a resuable component out of the budget form.我的所有功能都可以正常工作,所以我开始重构我的代码,以便从预算表格中制作一个可重复使用的组件。 I created a new component for the budget form.我为预算表创建了一个新组件。 In the budget form, I setup my state as shown below.在预算表中,我设置了我的 state,如下所示。 When the "create" form is used the budget props are null, as nothing is passed into the component.当使用“创建”表单时,预算道具是 null,因为没有任何东西传递到组件中。 When the "Edit" form is used the budget props are passed in.当使用“编辑”表单时,会传入预算道具。

state = {
formMode: this.props.formMode,
budget: {
  id: this.props.budget.BudgetId ? this.props.budget.BudgetId : "",
  name: this.props.budget.Name ? this.props.budget.Name : "",
  color: this.props.budget.Color
    ? this.props.budget.Color
    : this.backgroundColors[0],
  type: this.props.budget.Type ? this.props.budget.Type : "",
  startDate: this.props.budget.StartDate ? this.props.budget.StartDate : "",
  endDate: this.props.budget.EndDate ? this.props.budget.EndDate : "",
},
deviceLocale: "",
datepickerMode: "date",
showStartDatePicker: false,
showEndDatePicker: false,
};

I call the "Create" form from my parent component as shown below我从我的父组件调用“创建”表单,如下所示

<BudgetForm formMode={Constants.FORM_Mode_Add} />

I call the "edit" form from my parent component as shown below我从父组件调用“编辑”表单,如下所示

<BudgetForm formMode={Constants.FORM_Mode_Edit} budget={this.props.list}/>

When I open the form for "create" I get this error:当我打开“创建”表单时,出现此错误:

TypeError: undefined is not an object (evaluating '_this.props.budget.BudgetId')

I'm using this conditional ternary operator on my props in another form in my app and its working fine there.我在我的应用程序中以另一种形式在我的道具上使用这个条件三元运算符,并且它在那里工作正常。 I cannot seem to figure out what is the issue here?我似乎无法弄清楚这里有什么问题?

I solved my issue by changing the state object in my BudgetFrom component as shown below.我通过更改我的 BudgetFrom 组件中的 state object解决了我的问题,如下所示。

constructor(props) {
super(props);
this.state = {
  formMode: this.props.formMode,
  id: this.props.id ? this.props.id : "",
  name: this.props.name ? this.props.name : "",
  color: this.props.color ? this.props.color : backgroundColors[0],
  type: this.props.type ? this.props.type : "",
  startDate: this.props.startDate ? this.props.startDate : "",
  endDate: this.props.endDate ? this.props.endDate : "",
  deviceLocale: "",
  datepickerMode: "date",
  showStartDatePicker: false,
  showEndDatePicker: false,
};

} }

By not having the "budget" object within state as "child" object it seemed to fix the issue.通过在 state 中没有“预算” object 作为“子” object 似乎解决了这个问题。

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

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