【发布时间】:2016-09-26 08:03:04
【问题描述】:
我希望使用 Redux 验证表单。我正在尝试使用制作一个表单组件,它将遍历子组件并找到各种输入组件(不要与本机 <input> 混淆。
我知道有很多开源解决方案,但我想在开始选择之前了解一些机制。我有一个表单组件设置来测试这样的:
import React from 'react';
export default class Component extends React.Component {
componentDidMount() {
this._iterate(this.props.children);
}
render(){
return (
<form {...this.props}>{this.props.children}</form>
);
}
_iterate(children) {
React.Children.forEach(children, child => {
console.log(child);
if (child.props.children) {
console.log('get children');
this._iterate(child.props.children);
}
});
}
};
然后我有另一个具有这样渲染的组件:
render() {
return (
<div>
<Form>
<ComponentA />
<ComponentB />
</Form>
</div>
);
}
现在ComponentA 或ComponentB 可能有一个组件可以嵌套更多组件。在这些组件中将是我为 Text、Select 等制作的 React 组件。
上面的代码只是 console.log 组件,以及它们的任何子组件,在这个特定的渲染中。它不会跳入ComponentA children。
有解决办法吗?
【问题讨论】:
标签: reactjs react-redux