gpt4 book ai didi

javascript - Reactjs - cloneWithProps 重复的类名

转载 作者:行者123 更新时间:2023-12-02 16:16:25 25 4
gpt4 key购买 nike

我有一个 Accordion react 组件,但每次更新时,它都会创建类名的重复项。

我的代码:
AccordionListComponent 组件:

componentWillReceiveProps: function(nextProps) {
var accordions = this.state.accordions.map(function(accordion, i) {
nextProps.children[i].props.key = accordion.key;

var newAc = clone(accordion, nextProps.children[i].props);

return React.addons.update(accordion, {
$set: newAc
});
});

this.setState({
accordions: accordions
});
},

用法:

<AccordionListComponent>
<AccordionComponent className="date" title={dateTitle} index={0}>
<DateComponent updateDate={this.updateDate} />
</AccordionComponent>
</AccordionListComponent>

日志

Console logs

基于documentation来自 ReactJs,它指出

Do a shallow copy of element and merge any props provided by extraProps. 
The className and style props will be merged intelligently.

他们没有删除重复项吗?

谢谢。

最佳答案

看起来像那里 is no deduping logic :

function joinClasses(className/*, ... */) {
if (!className) {
className = '';
}
var nextClass;
var argLength = arguments.length;
if (argLength > 1) {
for (var ii = 1; ii < argLength; ii++) {
nextClass = arguments[ii];
if (nextClass) {
className = (className ? className + ' ' : '') + nextClass;
}
}
}
return className;
}

请注意cloneWithProps is deprecated支持新的 React.cloneElement,它不会为您合并类或样式。

关于javascript - Reactjs - cloneWithProps 重复的类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29530394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com