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

124
Views
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 answers
Answer question

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 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!