简体   繁体   English

Append 项目从一个数组到另一个数组,然后如果单击按钮,则将最后一项移回

[英]Append items from one array to another, then if button is clicked on, move the last item back

I'm building a function to help me with moving items from the arrays.我正在构建一个 function 来帮助我从 arrays 移动项目。 Component renders various sections:组件呈现各个部分:

  1. filledArray - an array of null's, that produces a dynamic amount of buttons depending on the length of the word.填充数组 - 一个空数组,根据单词的长度生成动态数量的按钮。
  2. bottomLetters - an array of letters, where the word has been split onto each individual letter. bottomLetters - 一个字母数组,其中单词已被拆分为每个单独的字母。 Once the letter is clicked on it will append a new item in filledArray.单击该字母后,append 将成为填充数组中的一个新项目。
  3. delete button - once clicked on, deletes the last item within the topLetter array and adds the back to the bottomLetters array..删除按钮 - 单击后,删除 topLetter 数组中的最后一项并将其添加到 bottomLetters 数组中。

Now I have two issues with the code:现在我的代码有两个问题:

  1. It does not update the render automatically.它不会自动更新渲染。 I'd have to save the code sandbox to generate the result.我必须保存代码沙箱才能生成结果。
  2. When clicking on delete button it breaks the value of the items in the bottomLetters array.单击删除按钮时,它会破坏 bottomLetters 数组中项目的值。 (For example try clicking on three items from bottomLetters array, hit save and then hit delete button, it won't move the items back to the array). (例如,尝试单击 bottomLetters 数组中的三个项目,点击保存,然后点击删除按钮,它不会将项目移回数组)。

This is the code example that I have: https://codesandbox.io/s/show-and-hide-in-react-forked-nh9l2?file=/src/MyApp.js这是我的代码示例: https://codesandbox.io/s/show-and-hide-in-react-forked-nh9l2?file=/src/MyApp.js

You need the state object for storing the local variable that needs to be automatically updated by React.您需要state object 来存储需要由 React 自动更新的局部变量。 To be able to use state object, you must convert your functional component code to class component .为了能够使用state object,您必须将功能组件代码转换为class 组件

Here are some changes I made to fix the problems.以下是我为解决问题所做的一些更改。

import React, { Component } from 'react';

export class myApp extends Component {
  state = {
    word: ['1234'],
    topLetters: [],
    bottomLetters: [],
    filledArray: [],
  };

  componentDidMount() {
    this.setState({
      bottomLetters: [...this.state.word[0]],
      filledArray: new Array(this.state.word[0].length).fill(null),
    });
  }

  handleLetters2Click = (letter, id) => {
    this.setState({ topLetters: [...this.state.topLetters, { letter }] });

    let bottomLetters = [...this.state.bottomLetters];
    bottomLetters.splice(id, 1);
    this.setState({ bottomLetters });
  };

  deleteLast = () => {
    if (!this.state.topLetters.length) return;

    const lastTopLetter = this.state.topLetters.length - 1;
    const lastLetter = this.state.topLetters[lastTopLetter].letter;

    let topLetters = [...this.state.topLetters];
    topLetters.pop();
    this.setState({ topLetters });

    let bottomLetters = [...this.state.bottomLetters];
    bottomLetters.push(lastLetter);
    this.setState({ bottomLetters });
  };

  render() {
    const { filledArray, topLetters, bottomLetters } = this.state;

    return (
      <div style={{ display: 'flex', flexDirection: 'column' }}>
        <div style={{ marginBottom: '40px' }}>
          {filledArray.map((index, id, item) => (
            <button key={id} className='item'>
              {topLetters[id] && topLetters[id].letter}
            </button>
          ))}
        </div>
        <div>
          {bottomLetters.map((index, id, item) => (
            <button
              key={id}
              className='item'
              onClick={() => this.handleLetters2Click(item[id])}
            >
              {item[id]}
            </button>
          ))}
        </div>
        <button onClick={this.deleteLast} style={{ marginTop: '40px' }}>
          DELETE
        </button>
      </div>
    );
  }
}

export default myApp;

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

相关问题 在 reactjs 中单击时,如何将按钮从一个 div 移动到另一个? - How to move a button from one div to another when clicked in reactjs? Javascript:尝试将项目从一个数组随机移动到另一个数组 - Javascript: Trying to randomly move items from one array to another 从数组中获取最后一次单击项的值 - Getting value of last clicked item from array 当单击另一个数组中的另一个单独的项目,同时又从第一个数组中删除该项目时,从一个数组中随机显示项目? - Show item at random from one array when another separate item in a different array is clicked while also removing that item from the first array? 将数组中的一项附加到一个 div - Append one item from an array to one div 如何将一个项目从一个列表移动到另一个列表? - How to move an item from one list to another? 如何有条件地将只有数字的项目从一个数组移动到另一个数组 - How to conditionally move only items with numbers from one array to another array jquery ExtJs 3.4:点击按钮,将所选项目从一个网格移动到另一个网格 - ExtJs 3.4 : Move selected items from one grid to another on a button click 从项目数组中删除一个项目只会删除最后一个项目 - Removing an item from an array of items only removes the last item 将数组中的项目移动到最后 position - Move item in array to last position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM