简体   繁体   English

React.js,如何在渲染时跟踪先前的值?

[英]React.js, How to track previous values while rendering?

new to Reactjs, so I'm trying to understand what concepts I'm missing. 是Reactjs的新功能,因此我试图了解我缺少的概念。

I have an array of values, say [1,1,1,2,2,2,3,3,3] that I would like to process it as: "1...2...3...", but I have no idea how to keep track of what the previous value that was rendered. 我有一个值数组,例如[1,1,1,2,2,2,3,3,3],我想将其处理为:“ 1 ... 2 ... 3 ...” ,但我不知道如何跟踪先前呈现的值。

My first thought was to set a state value to keep track what was processed. 我的第一个想法是设置一个状态值来跟踪处理的内容。 If the new value is the same as the previous, I can replace the text. 如果新值与之前的值相同,则可以替换文本。 But I can't set the state since it's in the rendering function: 但是我无法设置状态,因为它位于渲染函数中:

Warning: setState(...): Cannot update during an existing state transition (such as within render ). 警告:setState(...):在现有状态转换期间(例如在render内)无法更新。 Render methods should be a pure function of props and state. 渲染方法应该纯粹是props和state的函数。

So how can I keep track of the value being rendered, or is there a better way to do this? 那么我如何跟踪呈现的值,或者有更好的方法来做到这一点?

Example of what I tried: 我尝试过的示例:

var stuff = [1,1,1,2,2,2,3,3,3];

var foo = React.createClass({
  display: function(k) {
    return <line myStuff={k}>
  },
  render: function() {
    return (
      {Object.keys(stuff).sort().map(this.display)}
    )
  }
});

var line = React.createClass({
  getInitialState: function() {
    return { 
      currentValue: 0
    };
  },
  checkRepeat: function(value) {
    if (this.state.currentValue == value) {
      return '.'
    } else {
      this.setState({currentValue: value});
      return value;
    }
  },
  render: function() {
    <p>{this.checkRepeat(this.props.myStuff)}</p>
  }
});

Thank you! 谢谢!

What your code currently does: 您的代码当前正在做什么:

  • inside <Foo> component, Object.keys(stuff).sort() turns your array [1,1,1,2,2,2,3,3,3] into a different array, containing the keys, so result = ["0", "1", "2", "3", "4", "5", "6", "7", "8"] <Foo>组件内部, Object.keys(stuff).sort()将数组[1,1,1,2,2,2,3,3,3]转换为另一个包含键的数组,因此result = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]
  • each of these results is then used to create a new <Line> component, passing the "0", "1" etc as a prop to the component. 然后,将这些结果中的每一个用于创建新的<Line>组件,并将“ 0”,“ 1”等作为对组件的支持。
  • The intention of the code inside the <Line> component is probably to check if the number passed is unique (same a number from <Line> component before). <Line>组件内的代码的目的可能是检查传递的数字是否唯一(之前与<Line>组件相同)。 If so, render the number, otherwise, render "." 如果是这样,则渲染数字,否则,渲染“”。

Main issue with this code is that you are trying inside a child ( <Line> ), to compare its prop with a prop of a sibling child. 此代码的主要问题是,您正在一个孩子( <Line> )内部尝试将其道具与同级孩子的道具进行比较。

The best place to put this logic of comparing numbers, is inside the parent ( <Foo> ). 放置数字比较逻辑的最佳位置是父级( <Foo> )。
The child ( <Line> ) really should have simple and isolated logic: just render whatever prop the parent sends. 子级( <Line> )实际上应该具有简单且孤立的逻辑:只需渲染父级发送的任何道具。

To fix: 修理:

Change the render() function inside <Foo> to: <Foo>render()函数更改为:

render: function() {
  // copy the stuff array to a new array, 
  // where we replace all repeated values with a "." symbol
  var stuffCopy = stuff.map((item,i) => 
    { return (i==0 || item != arr[i-1]) ? item.toString() : "." });
  return (
    {stuffCopy.map(this.display)}
  )
}

Then you can simplify your <Line> component to: 然后,您可以将<Line>组件简化为:

var line = React.createClass({
  // this component no longer needs state
  // it can simply render the prop
  render: function() {
    <p>{this.props.myStuff}</p>
  }
});

PS: "Previous" can have two very different meanings: PS:“上一个”可以有两个非常不同的含义:

  1. "the value of the exact same variable at a previous moment in time": looking at one component, where the state or prop had value A, and then a new prop or state value of B is sent to the one component. “前一个时刻的完全相同的变量的值”:查看一个组件,状态或属性的值为A,然后将新的属性或状态B的值发送给该组件。
  2. "the value of the same variable, in another instance, rendered at the same time": if you render 9 components at the same time, previous value could mean, the value of the sibling component, rendered at the same moment, but appearing before this component in the list. “同一变量的值,在另一个实例中,同时渲染”:如果您同时渲染9个组件,则先前的值可能意味着兄弟组件的值同时渲染,但出现在之前该组件在列表中。

Your code suggests your question is about meaning nr. 您的代码表明您的问题与nr的含义有关。 2. (and my answer is focused on that meaning). 2.(我的回答集中在这个意思上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM