简体   繁体   English

如何使用React.js JavaScript在JSX代码中实现数组增量功能

[英]How to implement an array increment function within JSX code using React.js JavaScript

I'm trying to develop a React program that changes information in a component each time the button "rectForward" or "rectBackward" is pressed. 我正在尝试开发一个React程序,每次按下按钮“ rectForward”或“ rectBackward”时,它都会更改组件中的信息。 I'm passing the information in the form of an array of objects into my Body component from "importData" as seen below. 我将对象数组的形式的信息从“ importData”传递到我的Body组件中,如下所示。 That data is then converted into each object's indvidual data pieces through the map functions listed directly after render() is called. 然后,在调用render()之后直接通过列出的映射函数将该数据转换为每个对象的单个数据块。 What I want to happen when the rectForward button is pressed is for the "text1" array value in Column1 to incrament by 1. The same happens when rectBackward is pressed, but I want the value to decrement. 当我按下rectForward按钮时,我想发生的事情是使Column1中的“ text1”数组值递增1。当按下rectBackward时,也会发生同样的事情,但是我希望该值递减。 My primary difficulty is the syntax. 我的主要困难是语法。 As you can see in the statement onClick={Column1.text1=text1val[++] , this was my first attempt at implementing this functionality, but the syntax is definitely incorrect. 正如您在语句onClick={Column1.text1=text1val[++]看到的那样,这是我首次尝试实现此功能,但是语法绝对不正确。 I was wondering if I could get some help formatting this 我想知道是否可以得到一些格式化帮助

import React from "react";
import "./Body.css";
import Column1 from "./Body/Column1/Column1";
import "./Buttons.css";
import Column2 from "./Body/Column2/Column2";
import myData from "./myData";

    class Body extends React.Component {
        constructor() {
            super()
            this.state = {
              importData: myData
            }
          }

        render() {

            var ID = this.state.importData.map(item => item.id)
            var text1val = this.state.importData.map(item => item.text1)
            var text2val = this.state.importData.map(item => item.text2)
            var text3val = this.state.importData.map(item => item.text3)

            return(
                <div className="mainBody">
                    <div className="backPain">
                        <div className="holder">
                        <Column1 key={ID[0]} text1={text1val[0]}>
                        </Column1>

                        <div className="rectHolder">
                            <div className="rectForward" onClick={Column1.text1=text1val[++]}
                                <h2>Next</h2>
                            </div>
                            <div className="rectBackward">
                                <h2>Prev</h2>
                            </div>
                        </div>

                        <Column2 key={ID[0]} text2={text2val[0]} text3={text3val[0]}>
                        </Column2>
                        </div>
                    </div>
                </div>
            )
        }
    }

    export default Body;

Thanks so much! 非常感谢!

Simple thing i will do is keep an index in state. 我要做的简单的事情就是保持索引状态。 Than pass a function to next and prev which takes care of changing this index. 比将函数传递给next和prev负责更改该索引。 and will show the values based on current state. 并显示基于当前状态的值。

this.state = {
  currentIndex : 0
}

HandleCurrentIndex(type){
  if(type === 'inc'){
    this.setState({currentIndex: this.state.currentIndex++})
  } else {
   this.setState({currentIndex: this.state.currnIndex-1 })
  }
}

 <div className="rectForward" onClick={()=>this.HandleCurrentIndex("inc")}>
     <h2>Next</h2>
  </div>
 <div className="rectBackward" onClick={()=>this.HandleCurrentIndex('dec')}>
     <h2>Prev</h2>
 </div>

On side note:- This is just and example in product you should take care of index going below zero as well index exceeding limits of your data. 附带说明:-这只是产品中的一个例子,您应该注意索引低于零以及索引超出数据限制。 which in turn will show undefined values. 依次显示未定义的值。 simple thing you should do is whenever it goes out of limit just reset it to default value ( 0 ) 您应该做的简单事情是,只要超出限制,就将其重置为默认值(0)

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

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