我有一个组件(ChoicerGroup),该组件接收许多孩子(Choicer)作为列表。该组件克隆了这个孩子,并添加了两个属性:'onClick'(func)和'checkin'(string);
在开发模式下,属性正确地在子级中增加了,已经在构建中,这两个属性没有增加。
我正在使用create-react-app,Mac osx,chorme最新版本; “反应脚本”:“ ^ 2.1.5” “ build”:“反应脚本建立”, “浏览者列表”:[ “> 0.2%”, “没死”, “不是,即<= 11”, “不是全部op_mini” ]
<ChoicerGroup
checkin={props.checkin}
onClick={props.onChange}>
{props.answers.map((pAnswer, pIndex) => (
<Choicer
key={pIndex}
id={pAnswer.name}
name={pAnswer.name}
value={pAnswer.value}>
{pAnswer.label}
</Choicer>
))}
</ChoicerGroup>
class Choicer extends Component {
constructor(props) {
super(props);
this.state = {
isChecked: false
};
}
handleClick = pEvent => {
this.setState({ isChecked: true });
this.props.onClick && this.props.onClick(pEvent);
};
componentDidMount() {
console.log(this.props);
this.setState({
isChecked: this.props.checkin === this.props.value
});
}
componentDidUpdate(prevProps) {
if (prevProps.checkin !== this.props.checkin) {
this.setState({
isChecked: this.props.checkin === this.props.value
});
}
}
render() {
const xClass = classNames(Style.root, this.props.className, {
[Style.active]: this.state.isChecked
});
return (
<button
type={'button'}
value={this.props.value}
className={xClass}
onClick={e => this.handleClick(e)}>
{this.props.children}
</button>
);
}
}
class ChoicerGroup extends Component {
constructor(props) {
super(props);
this.state = {
isChecked: null
};
}
handleClick = pEvent => {
this.setState({ isChecked: pEvent.target.value });
this.props.onClick && this.props.onClick.choice(pEvent);
};
componentDidMount() {
this.setState({
isChecked: this.props.checkin
});
}
componentDidUpdate(prevProps) {
}
render() {
const childrenWithParentProps = React.Children.map(
this.props.children,
child => {
if (child.type.name === 'Choicer') {
console.log(child);
return React.cloneElement(child, {
onClick: this.handleClick,
checkin: this.state.isChecked || 0
});
} else {
return React.cloneElement(child);
}
}
);
return <div className={Style.root}>{childrenWithParentProps}</div>;
}
}
我只希望具有与构建中的开发模式相同的行为