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

263
Views
How to force new state in React (Hooks) for textarea when clicking on another element?
  • codesandbox.io sandbox
  • github.com repository

I am creating this small Wiki project.

My main component is Editor(), which has handleClick() and handleModeChange() functions defined.

handleClick() fires when a page in the left sidebar is clicked/changed.

handleModeChange() switches between read and write mode (the two icon buttons in the left sidebar).

When in read mode, the clicking on different pages in the left sidebar works properly and changes the main content on the right side.

However, in write mode, when the content is echoed inside <TextareaAutosize>, the content is not changed when clicking in the left menu.

In Content.js, I have:

<TextareaAutosize
  name="textarea"
  value={textareaValue}
  minRows={3}
  onChange={handleMarkdownChange}
/>

textareaValue is defined in Content.js as:

const [textareaValue, setTextareaValue] = useState(props.currentMarkdown);

const handleMarkdownChange = e => {
  setTextareaValue(e.target.value);
  props.handleMarkdownChange(e.target.value);
};

I am unsure what is the correct way to handle this change inside textarea. Should I somehow force Editor's child, Content, to re-render with handleClick(), or am I doing something completely wrong and would this issue be resolved if I just changed the definition of some variable?

I have been at this for a while now...

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You just need to update textareaValue whenever props.currentMarkdown changes. This can be done using useEffect.

useEffect(() => {
  setTextareaValue(props.currentMarkdown);
}, [props.currentMarkdown]);

Edit reverent-breeze-cwyfw


Problem:

const [textareaValue, setTextareaValue] = useState(props.currentMarkdown);

useState will use props.currentMarkdown as the initial value but it doesn't update the current state when props.currentMarkdown changes.


Unrelated:

The debounce update of the parent state can be improved by using useRef

const timeout = useRef();

const handleMarkdownChange = (newValue) => {
    if (timeout.current) {
        clearTimeout(timeout.current);
    }
    timeout.current = setTimeout(function () {
        console.log("fire");
        const items = [];
        for (let value of currentData.items) {
            if (value["id"] === currentData.active) {
                value.markdown = newValue;
                value.unsaved = true;
            }
            items.push(value);
        }
        setCurrentData({ ...currentData, items: items });
    }, 200);
};

using var timeout is bad because it declares timeout on every render, whereas useRef gives us a mutable reference that is persisted across renders

about 4 years ago · Santiago Trujillo 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!