Reactjs 呈现时返回未定义的表单属性
我使用一个道具来呈现错误,这些错误只有在点击提交按钮后返回表单时才会触发。但是,当窗体加载时,此功能正在工作。我遇到控制台错误Reactjs 呈现时返回未定义的表单属性,reactjs,Reactjs,我使用一个道具来呈现错误,这些错误只有在点击提交按钮后返回表单时才会触发。但是,当窗体加载时,此功能正在工作。我遇到控制台错误无法读取未定义的属性“company” 路径:SingleInput道具 const SingleInput = (props) => ( <FormGroup validationState={props.error ? 'error' : null}> {props.error ? <HelpBlock>{props.erro
无法读取未定义的属性“company”
路径:SingleInput道具
const SingleInput = (props) => (
<FormGroup validationState={props.error ? 'error' : null}>
{props.error ? <HelpBlock>{props.error}</HelpBlock> : '' }
</FormGroup>
);
SingleInput.propTypes = {
error: PropTypes.string
};
问题就在这里careerHistoryPosition.错误['company']
careerHistoryPosition是一个数组,您正在写入careerHistoryPosition。错误,当您试图从数组中获取密钥时,这些错误将是未定义的。然后您尝试从undefined中获取company的值。OP在哪里提到了
careerHistoryPosition
是一个数组。我认为您将其与CareerHistoryPositions
混淆,但通过查看错误,无法读取未定义的属性公司,我认为这可能是渲染中的一个输入错误。我看到两个错误,您需要将贴图函数包装在一个div中,因为您只能从渲染返回一个元素,并且键={careerHistoryPosition.uniqueId}
是一个问题,因为对象中没有定义uniqueId
。我有很多代码。这两件事都在我的完整标记中。我得到的错误与error={careerHistoryPosition.errors['company']}
我尝试过类似error={!!careerHistoryPosition.errors['company']}
这也不起作用。我的困惑是,当它第一次呈现时,它应该是未定义的
,因为在该点上没有错误。它只在提交表单时返回一个错误。你能在MAPI中记录CarreHistoryPostion对象吗?如果数据库中存储了数据,则不能表单已填充。因此careerHistoryPositions工作正常。这是否与构造函数中的空数组错误有关。这是否应该有所不同?因此错误与我的构造函数有关。我需要在错误中定义公司
。修复很简单,careerHistoryPositions:[{company:'',错误:{公司:'}}],
constructor(props) {
super(props);
this.state = {
careerHistoryPositions: [{company: '', errors: {} }],
};
}
render() {
return (
{this.state.careerHistoryPositions.map((careerHistoryPosition) => (
<div key={careerHistoryPosition.uniqueId} className="individual-position">
<SingleInput
error={careerHistoryPosition.errors['company']}
/>
</div>
))}
)
}