javascript - 单击时显示数组中的最后一个索引(在持续更新的数组中)

标签 javascript reactjs indexing array.prototype.map

我的申请即将结束。

因此对于银行帐户来说,他们会要求您提供密码的第一个字母,然后是例如第四个

我厌倦了依靠自己,所以我创建了这个应用程序。

但还有最后一个错误,我不知道如何修复。

所以当我按“1”时,我得到“1 - H”,
然后当我按“4”时,我想得到:

“1 - H”(之前点击)
“4 - X”(刚刚点击)

但相反,我得到:

“4 - X”(刚刚点击)
“4 - X”(刚刚点击)

所以这是由 handleResults() 函数在我的 Input 组件中的工作方式引起的,但目前我唯一的想法是如何处理这个...

import React, { Component } from 'react';
import TextField from 'material-ui/TextField';

import './style.css';

import Buttons from '../Buttons';
import Results from '../Results';

class Input extends Component {
  constructor(props) {
    super(props);
    this.state = {
      password: 'Hh9Xzke2ayzcEUPHuIfS',
      selectedButtons: [],
    };
    this.handleButtonSelectTwo = this.handleButtonSelectTwo.bind(this);
  }

  handleInputChange(pass) {
    this.setState({ password: pass });
  }

  handleButtonSelectTwo(selected) {
    this.setState({
      selectedButtons: [...this.state.selectedButtons, selected],
    });
  }

  handleResults() {
    return this.state.selectedButtons.map(el => (
      <Results key={el} appState={this.state} />
    ));
  }

  render() {
    return (
      <div>
        <div className="Input-textfield">
          <TextField
            hintText="Paste your password here to begin"
            value={this.state.password}
            onChange={event => this.handleInputChange(event.target.value)}
          />
        </div>
        <div>
          <Buttons
            handleButtonSelectOne={this.handleButtonSelectTwo}
            array={this.state.password.length}
          />
          {this.handleResults()}
        </div>
      </div>
    );
  }
}
export default Input;

这是结果组件代码:

import React, { Component } from 'react';
import _ from 'lodash';

import Avatar from 'material-ui/Avatar';
import List from 'material-ui/List/List';
import ListItem from 'material-ui/List/ListItem';

import './style.css';

const style = {
  avatarList: {
    position: 'relative',
    left: -40,
  },
  avatarSecond: {
    position: 'relative',
    top: -40,
    left: 40,
  },
};

class Results extends Component {
  resultsEngine(arg) {
    const { selectedButtons, password } = this.props.appState;
    const passwordArray = password.split('').map(el => el);

    const lastSelectedButton = _.last(selectedButtons);
    const passwordString = passwordArray[_.last(selectedButtons) - 1];

    if (arg === 0) {
      return lastSelectedButton;
    }

    if (arg === 1) {
      return passwordString;
    }

    return null;
  }
  render() {
    if (this.props.appState.selectedButtons.length > 0) {
      return (
        <div className="test">
          <List style={style.avatarList}>
            <ListItem
              disabled
              leftAvatar={<Avatar>{this.resultsEngine(0)}</Avatar>}
            />
            <ListItem
              style={style.avatarSecond}
              disabled
              leftAvatar={<Avatar>{this.resultsEngine(1)}</Avatar>}
            />
          </List>
        </div>
      );
    }
    return <div />;
  }
}

export default Results;

任何人都知道我应该如何更改 handleResults() 函数中的代码以实现我的目标?任何解决该问题的帮助将不胜感激。

按钮组件代码:

import React from 'react';

import OneButton from '../OneButton';

const Buttons = props => {
  const arrayFromInput = props.array;
  const buttonsArray = [];

  for (let i = 1; i <= arrayFromInput; i++) {
    buttonsArray.push(i);
  }

  const handleButtonSelectZero = props.handleButtonSelectOne;

  const allButtons = buttonsArray.map(el => (
    <OneButton key={el} el={el} onClick={handleButtonSelectZero} />
  ));

  if (arrayFromInput > 0) {
    return <div>{allButtons}</div>;
  }

  return <div />;
};

export default Buttons;

OneButton代码:

import React, { Component } from 'react';
import RaisedButton from 'material-ui/RaisedButton';

const style = {
  button: {
    margin: 2,
    padding: 0,
    minWidth: 1,
  },
};

class OneButton extends Component {
  constructor() {
    super();
    this.state = { disabled: false };
  }

  handleClick() {
    this.setState({ disabled: !this.state.disabled });
    this.props.onClick(this.props.el);
  }

  render() {
    return (
      <RaisedButton
        disabled={this.state.disabled}
        key={this.props.el}
        label={this.props.el}
        style={style.button}
        onClick={() => this.handleClick()}
      />
    );
  }
}

export default OneButton;

最佳答案

在 Results 组件的 resultsEngine 函数中,您指定始终希望使用 _.last(selectedButtons)。这就是它正在做的,因此你总是看到最后一个按钮被点击。您真正想要的是要显示的迭代索引。

const lastSelectedButton = selectedButtons[this.props.index];
const passwordString = passwordArray[selectedButtons[this.props.index]];

要获得一个索引,您必须创建并传入一个索引,因此当您在 Input 组件的 handleResults 函数中映射选定的 Button 时创建它。

handleResults() {
    return this.state.selectedButtons.map((el, index) => (
      <Results key={el} appState={this.state} index={index} />
    ));
  }

关于javascript - 单击时显示数组中的最后一个索引(在持续更新的数组中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717839/

相关文章:

javascript - javascript中是否存在对象/数组

javascript - 函数在调用时运行正常,但始终返回未定义

javascript - MUI 下一个工具提示在悬停时不显示

javascript - React : Dynamic number of stateful items, 又名处理 "Rendered more hooks than during the previous render"

python - 查找列表中连续项目的索引

javascript - 使用表单输入设置 cookie

javascript - IE 不正确地呈现动态内容,直到进行样式表更改

mysql - 为具有多个 OR 条件和 AND 条件的选择查询创建索引

indexing - 什么索引可以加快 X-Hive/Documentum xDB 中的 XQuery?

javascript - TypeError : server. View 不是 hapi.js 中的函数