【问题标题】:The value of 'index' keeps to be 0 in 'onChange' while looping through material-ui rating component在循环通过material-ui评级组件时,'onChange'中'index'的值保持为0
【发布时间】:2021-10-07 06:49:21
【问题描述】:

我正在尝试在我的电子商务 react-js 网络应用中显示每个产品的评分值。我能够通过 JS 地图功能的索引跟踪/处理哪个产品已悬停。 ('onChangeActive' 是这个处理程序)

 {products.map((product, index) => (

虽然,“onChange”总是为“index”函数参数发送 0,即使我从数组列表中单击任何产品的星号。结果,我无法提供明星功能

    <Rating
    name="hover-feedback"
    value={this.state.rateValue[index] === undefined ?
        product.rating_sum: this.state.rateValue[index]}
    precision={0.5}
    onChange={(event, newValue) => { // OnClick handler
        this.setValue(newValue, index);
    }}
    onChangeActive={(event, newValue) => { // onHover handler
        this.setHover(newValue, index);
    }}
/>
{this.state.rateHover[index] !== undefined && <Box ml={2}>
    {labels[this.state.rateHover[index] !== undefined ?
                                                              this.state.rateHover[index] : this.state.rateValue[index]]}</Box>}

设置值函数

    setValue = (newValue, index) =>{

    console.log("value", index, newValue)

    if(newValue !== -1 && newValue!== null) {
        let oldValue = this.state.rateValue;
        oldValue[index] = newValue;

        this.setState({
                rateValue: oldValue,

        })
    }
}

【问题讨论】:

  • 将您的代码粘贴为文本,而不是图像。包括定义索引的部分。
  • 我已经发布了我在第一部分中声明“索引”的位置。此外,图像已替换为代码。
  • setValue 是做什么的?
  • 'setValue' 和 'setHover' 都更新/设置每个产品的悬停/点击起始值的值。我将这些起始值保留为与产品直接对应的数组。像这样:product= [p0, p1, p2 .....] rateValue = [p0, p1, 2 .....] hoverValue = [p0, p1, p2 .... 无论如何,我得到了0 表示 onChange 期间的索引

标签: javascript arrays reactjs material-ui


【解决方案1】:

您应该使用 React useState,而不是尝试使用自定义状态定义重新发明轮子。 https://reactjs.org/docs/hooks-state.html

// Get a hook function
const {
  useState
} = React;

const Example = ({
  title
}) => {
  const [state, setState] = useState({
    rateValue: [0, 0, 0, 0, 0]
  });

  const products = [{
      rating_sum: 4
    },
    {
      rating_sum: 6
    },
    {
      rating_sum: 5
    },
  ]

  const setValue = (event, index) => {

    const newValue = event.target.value

    console.log("value", index, newValue)

    if (newValue !== -1 && newValue !== null) {
      const {
        rateValue
      } = state
      setState({
        rateValue: [
          ...rateValue.slice(0, index), newValue, ...rateValue.slice(index + 1)
        ]
      })
    }
  }
  return ( <
    div > {
      products.map((product, index) => {
        return <div >
          <
          input value = {
            index
          }
        type = 'number'
        onChange = {
          (event, newValue) => {
            setValue(event, index)
          }
        }
        /> < /
        div >
      })
    } < div > {
      state.rateValue
    } < /div>< /
    div >
  );
};

// Render it
ReactDOM.render( <
  Example title = "Example using Hooks:" / > ,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

【讨论】:

  • 感谢您的建议。寿,问题甚至在处理状态之前。正如您在“setValue”方法中看到的那样,我正在通过控制台记录结果,并且索引值始终为 0
  • 什么是索引?你永远不会显示初始化。我的观点是;使用处理状态的内置方法。
  • 我已经贴出了索引初始化的地方。 {products.map((product, index) => (这是 JS map 内置机制,用于在循环期间跟踪数组的索引。
  • 编辑了我的答案
猜你喜欢
  • 1970-01-01
  • 2018-12-19
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
相关资源
最近更新 更多