【发布时间】:2020-01-20 15:45:04
【问题描述】:
我将 props 变量分配给组件变量,当我更改组件变量时,我更改了 props...
父组件:
const prova = [
{
1: 'a'
},
{
2: 'b'
},
{
3: 'c'
},
{
4: 'd'
},
{
5: 'e'
}
]
class SearchScreen extends React.Component {
...
<ScrollHorizontalTwoColumns categories={prova} title="Your Categories" seeAll={true}/>
...
子组件:
class ScrollHorizontalTwoColumns extends React.Component {
categories1 = "";
categories2 = "";
constructor(props) {
super(props);
console.warn(this.props.categories);
if (typeof this.props.categories === 'object' && this.props.categories.length > 0){
this.categories1 = this.props.categories;
this.categories2 = this.categories1.splice(0, Math.ceil(this.categories1.length / 2))
}
console.warn(this.props.categories);
}
第二个警告与第一个警告不同...
【问题讨论】:
-
我似乎错过了您的问题/问题。
-
@epascarello 对象是通过引用传递的。
.splice弄乱了他传入的数组this.props.categories -
@Thomas 已经解决了。它必须与 [... this.props.categories] 一起发送
-
@Cookie 请添加“已解决”信息作为答案
-
请发布您的解决方案作为答案。我现在正在回滚您的编辑。不应将解决方案编辑到问题中。
标签: javascript react-native components