score:0

in your componenent you used componentdidmount to fetch data.

you could the useeffect hook in your function to have the same effect

https://reactjs.org/docs/hooks-effect.html

useeffect takes two parameters, a function that gets called and a dependencies array.

if you pass in an empty array into as dependencies, your function gets only called on after your function gets mounted

const [assettype, setassettype] = usestate(null)
useeffect( () => {
  assettypeservice.getassettypebyid(assettype_assettypeid).then((res) => {
    setassettype(res.data);
  });
}, [])
if(!assettype)
  return (<ul>loading...</ul>
return (<ul>assettype.assettypename</ul>)

score:1


new function code:
...

   import react, { usestate, useeffect} from 'react';
   import assettypeservice from './assettypeservice'
   
   function getassettypenamefunction() {
   const [assettype, setassettype] = usestate(null)
   useeffect( () => {
     assettypeservice.getassettypebyid(assettype_assettypeid).then((res) => {
       setassettype(res.data);
     });
   }, []);
   if(!assettype)
     return (<ul>loading...</ul>)
   return (<ul>assettype.assettypename</ul>);
   }
   export default getassettypenamefunction;

...

Related Query

More Query from same tag