Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
State becomes empty

Counter File

import React, { useState } from "react";
import Widget from "./widget";

const Counter = () => {
const [form, setForm] = useState(<></>)
const [text, setText] = useState("")

const onCounterChange =() => {
    setText(text)
}
const formLoad =() =>{
    setForm(
        <Widget 
    onCounterChange={onCounterChange}
    children={
        
        <input type="text" onChange={(e) =>{
            setText(e.target.value)
        }}/>
        
    }
    />
    )
}
return (
    <div>
        {text}
    <button onClick={formLoad}>
        load widget
    </button>
    {form}
    </div>
)
}

export default Counter

Widget File

import React from 'react'

export default function Widget(props) {
  return (
    <div className="buttons">
        {props.children}
        <button onClick={props.onCounterChange}>Save</button>
    </div>
  )
}

I have created small text printing page . for some purpose I have added children in a diff component and handling widget in a state , so when I try to change the data , text state is changing but when I click save text state becomes empty

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As I mentioned in my comment putting a component in state probably isn't the best way of approaching this. Instead I would have a boolean state that allows you to toggle the component on/off.

const { useState } = React;

function Example() {

  const [ showWidget, setShowWidget ] = useState(false);
  const [ text, setText ] = useState('');

  function handleChange(e) {
    setText(e.target.value);
  }

  function handleClick() {
    setShowWidget(!showWidget);
  }
  
  function handleSave() {
    console.log(`Saved state: ${text}`);
  }

  return (
    <div>
      
      <p className="text">Current state: {text}</p>
      
      <button onClick={handleClick}>
        Load widget
      </button>
      
      {showWidget && (
        <Widget>
          <input
            type="text"
            onChange={handleChange}
          />
          <button onClick={handleSave}>Save</button>
        </Widget>
      )}
    
    </div>
  );
}

function Widget({ children }) {
  return <div className="widget">{children}</div>;
}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
.widget { margin-top: 1em; }
.text { margin-bottom: 1em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda