【问题标题】:By assigning prop variables to component variables they are linked通过将 prop 变量分配给组件变量,它们被链接
【发布时间】: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


【解决方案1】:

解决方案是:

它必须改变 this.categories1 = this.props.categories; for this.categories1 = [...this.props.categories];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2011-09-25
    • 2018-12-19
    • 2016-12-12
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多