{"id":12898,"date":"2025-07-23T10:42:13","date_gmt":"2025-07-23T10:42:13","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12898"},"modified":"2025-07-23T10:44:17","modified_gmt":"2025-07-23T10:44:17","slug":"finddomnode-is-deprecated-in-strictmode","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/finddomnode-is-deprecated-in-strictmode","title":{"rendered":"findDOMNode is Deprecated in StrictMode"},"content":{"rendered":"<h2>Problem<\/h2>\n<p>Using ReactDOM.findDOMNode() like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\">\r\nconst node = ReactDOM.findDOMNode(this);\r\n<\/pre>\n<p>Will throw a warning in <strong>StrictMode:<\/strong><\/p>\n<p><strong>Warning: findDOMNode is deprecated in StrictMode.<\/strong><\/p>\n<h2>Recommended Alternatives<\/h2>\n<h3>1. Use ref Instead (Functional or Class Components)<\/h3>\n<p><strong>Functional Component (with useRef)<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\">\r\nimport { useRef, useEffect } from 'react';\r\nfunction MyComponent() {\r\n  const divRef = useRef<HTMLDivElement>(null);\r\n  useEffect(() => {\r\n    if (divRef.current) {\r\n      divRef.current.focus(); \/\/ example usage\r\n    }\r\n  }, []);\r\n  return <div ref={divRef}>Hello<\/div>;\r\n}\r\n<\/pre>\n<p><strong>Class Component (with createRef)<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\">\r\nclass MyComponent extends React.Component {\r\n  constructor(props) {\r\n    super(props);\r\n    this.myRef = React.createRef();\r\n  }\r\n  componentDidMount() {\r\n    if (this.myRef.current) {\r\n      this.myRef.current.focus(); \/\/ or do something else\r\n    }\r\n  }\r\n  render() {\r\n    return <div ref={this.myRef}>Hello<\/div>;\r\n  }\r\n}\r\n<\/pre>\n<h3>2. For 3rd-party Libraries Using findDOMNode<\/h3>\n<p>If you&#8217;re using a library (e.g., Material-UI, React Transition Group) that internally uses findDOMNode, you can usually:<\/p>\n<ul>\n<li>Use ref-forwarding versions of components<\/li>\n<li>Avoid wrapping the component in StrictMode (not ideal long-term)<\/li>\n<li>Update the library if newer versions support ref properly<\/li>\n<\/ul>\n<h3>Why Avoid findDOMNode?<\/h3>\n<ul>\n<li>Breaks encapsulation: can access DOM of other components<\/li>\n<li>Not compatible with React&#8217;s future architecture (like concurrent mode)<\/li>\n<li>Often a sign of an anti-pattern in component design<\/li>\n<\/ul>\n<p>You are updating title directly on the component\u2019s state, <strong>but the state only contains<\/strong> editTodo, not a top-level title field. That\u2019s why your function seems \u201cnot working\u201d \u2014 you\u2019re updating the wrong part of the state.<\/p>\n<h2>Correct Approach<\/h2>\n<p>Since editTodo is part of the state, and title is a nested property inside it, you need to update it like this:<\/p>\n<h3>Fix handleChange method:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\">\r\nhandleChange = (event) => {\r\n  this.setState(prevState => ({\r\n    editTodo: {\r\n      ...prevState.editTodo,\r\n      title: event.target.value\r\n    }\r\n  }));\r\n}\r\n<\/pre>\n<p>This properly updates editTodo.title in the state, and now value={this.state.editTodo.title} will reflect what you type.<\/p>\n<h2>Bonus: Initialize editTodo from Props When Editing<\/h2>\n<p>Right now, editTodo.title is initially &#8220;&#8221;. So the edit field is empty when you open the modal. You may want to set it from props when handleShow is called:<\/p>\n<h3>Update handleShow:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\">\r\nhandleShow = () => {\r\n  const { id, title, isCompleted } = this.props.todo;\r\n  this.setState({\r\n    show: true,\r\n    editTodo: { id, title, isCompleted }\r\n  });\r\n}\r\n<\/pre>\n<h2>Summary of Fixes<\/h2>\n<ul>\n<li>Update handleChange to modify editTodo.title, not title at the top level.<\/li>\n<li>Populate editTodo when opening the modal using this.props.todo.<\/li>\n<\/ul>\n<h2>Console Warning: &#8220;Type &#8216;void&#8217; is not assignable to type &#8216;ReactNode'&#8221;<\/h2>\n<p>This error could occur if you&#8217;re accidentally using a function where JSX is expected, for example:<\/p>\n<pre class=\"lang:markup\">&lt;p&gt;{this.props.handleDelete(id)}&lt;\/p&gt;&gt; \/\/ \u274c BAD<\/pre>\n<p>This returns void and is invalid.<\/p>\n<p>Instead:<\/p>\n<pre class=\"lang:markup\">&lt;p onClick={() =&gt; this.props.handleDelete(id)}&gt;Delete&lt;\/p&gt; \/\/ \u2705 GOOD<\/pre>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/flash-messages-in-nodejs\" target=\"_blank\">Flash Messages in NodeJS<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Using ReactDOM.findDOMNode() like this: const node = ReactDOM.findDOMNode(this); Will throw a warning in StrictMode: Warning: findDOMNode is deprecated in StrictMode. Recommended Alternatives 1. Use ref Instead (Functional or Class Components) Functional Component (with useRef) import { useRef, useEffect } from &#8216;react&#8217;; function MyComponent() { const divRef = useRef(null); useEffect(() => { if (divRef.current) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12899,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-12898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12898"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12898"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12898\/revisions"}],"predecessor-version":[{"id":12902,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12898\/revisions\/12902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12899"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}