我在控制台中收到上述错误,即使我正确地返回了要渲染的组件。errorTexts = ["email is invalid"]
以下代码中的值
if(@state.errorTexts.length>0)
dangerouslySetInnerHTML: {
__html: ReactDOMServer.renderToString(
React.createElement AdminError, errorTexts: @state.errorTexts
)
}
AdminError 组件包含以下代码:
#app/assets/javascripts/components/adminerror.js.coffee
@AdminError = React.createClass
getDefaultProps: ->
errorTexts: []
render: ->
for errorText in @props.errorTexts
dom.div
className: 'help-block'
errorText
这个组件的 JS 等价物是:
(function() {
this.AdminError = React.createClass({
getDefaultProps: function() {
return {
errorTexts: []
};
},
render: function() {
var errorText, i, len, ref, results;
ref = this.props.errorTexts;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
errorText = ref[i];
results.push(dom.div({
className: 'help-block'
}, errorText));
}
return results;
}
});
}).call(this);
从 AdminError 组件返回的结果值显示在下面的屏幕截图中,但是,我不确定返回的对象类型是否可以接受,因为它在 for 循环中:
更新: 修改了 AdminError 组件中的代码以修复此错误
#app/assets/javascripts/components/adminerror.js.coffee
@AdminError = React.createClass
getDefaultProps: ->
errorTexts: []
render: ->
dangerouslySetInnerHTML: {
__html: marked((
dom.div null,
for errorText, index in @props.errorTexts
dom.div
key: index
className: 'help-block'
errorText
).toString())
}
并再次收到此错误:
Uncaught Invariant Violation: ReactCompositeComponent.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.invariant