Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Agregue elementos de una matriz a otra, luego, si se hace clic en el botón, mueva el último elemento hacia atrás

Estoy creando una función que me ayude a mover elementos de las matrices. El componente representa varias secciones:

  1. filledArray: una matriz de valores nulos que produce una cantidad dinámica de botones según la longitud de la palabra.
  2. bottomLetters: una matriz de letras, donde la palabra se ha dividido en cada letra individual. Una vez que se hace clic en la letra, se agregará un nuevo elemento en el arreglo relleno.
  3. botón Eliminar: una vez que se hace clic en él, elimina el último elemento dentro de la matriz topLetters y agrega la parte posterior a la matriz bottomLetters.

Ahora tengo dos problemas con el código:

  1. No actualiza el render automáticamente. Tendría que guardar el código sandbox para generar el resultado.
  2. Al hacer clic en el botón Eliminar, rompe el valor de los elementos en la matriz bottomLetters. (Por ejemplo, intente hacer clic en tres elementos de la matriz bottomLetters, presione guardar y luego presione el botón Eliminar, no moverá los elementos nuevamente a la matriz).

Este es el ejemplo de código que tengo: https://codesandbox.io/s/show-and-hide-in-react-forked-nh9l2?file=/src/MyApp.js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Necesita el objeto de state para almacenar la variable local que React debe actualizar automáticamente. Para poder usar el objeto de state , debe convertir su código de componente funcional en componente de clase .

Aquí hay algunos cambios que hice para solucionar los problemas.

 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;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!