I brought the code below for easy understanding. Note the location of the a function.
There are declarative differences depending on where the a function is in React.
Both are code that works correctly.
Are there any benefits of defining them within React components?
function a(init, sub_name, value) { let t2 = cloneDeep(init); t2[sub_name] = value; return t2; } export default function test(props) { const [obj, setobj] = useState({ first: { id: 123, v: 456 }, }); return ( <div> <Test onUpdateData={(v) => setobj((p) => a(p, "first", v))} /> </div> ); } export default function test(props) { const [obj, setobj] = useState({ first: { id: 123, v: 456 }, }); function a(init, sub_name, value) { let t2 = cloneDeep(init); t2[sub_name] = value; return t2; } return ( <div> <Test onUpdateData={(v) => setobj((p) => a(p, "first", v))} /> </div> ); } - This is a shortened code that makes it easy to understand the intent of the question.