What can I use in functional components to have same behavior as componentDidMount?

My UI was working fine until it was using a class component. Now I am refactoring it to a functional component.

I have to load my UI based on the data I receive from an API handler. My UI will reflect the state of the camera which is present inside a room. Every time the camera is turned on or off from the room, I should receive the new state from the API apiToGetCameraState.

I want the console.log present inside the registerVideoStateUpdateHandlerWrapper to print both on UI load for the first time and also to load every time the video state is changed in the room. However, it doesn’t work when the UI is loaded for the first time.

This is how my component looks like:

const Home: React.FunctionComponent<{}> = React.memo(() => {
  const  = React.useState(true);

  const registerVideoStateUpdateHandlerWrapper = React.useCallback(() => {
      (videoState: boolean) => {
        // this log does not show up when the UI is loaded for the first time
          `Video value before updating the state: ${video} and new state is: ${videoState} `
  }, );

  React.useEffect(() => {
    //this is getting called when the app loads
    alert(`Inside use effect for Home component`);
    registerVideoStateUpdateHandlerWrapper ();
  }, [registerVideoStateUpdateHandlerWrapper ]);

  return (

This was working fine when my code was in class component. This is how the class component looked like.

class Home extends Component {
    registerVideoStateUpdateHandlerWrapper = () => {
        apiToGetCameraState((videoState) => {
            console.log(`ToggleVideo value before updating the state: ${this.state.toggleCamera} and new state is: ${videoState}`);
    setStateWrapper = (toggleCameraUpdated) => {
        console.log("Inside setStateWrapper with toggleCameraUpdated:" + toggleCameraUpdated);
            toggleCamera: (toggleCameraUpdated === "true" ) ? "on" : "off",
    constructor(props) {
        this.state = {
            toggleCamera: false,
    componentDidMount() {
        console.log(`Inside componentDidMount with toggleCamera: ${this.state.toggleCamera}`)
        this.registerVideoStateUpdateHandlerWrapper ();   
    render() {
      return (
            <Camera isVideoOn={this.state.toggleCamera} />

What all did I try?

  1. I tried removing the useCallback in the registerVideoStateUpdateHandlerWrapper function and also the dependency array from React.useEffect and registerVideoStateUpdateHandlerWrapper. It behaved the same
  2. I tried updating the React.useEffect to have the code of registerVideoStateUpdateHandlerWrapper in it but still no success.

169 thoughts on “What can I use in functional components to have same behavior as componentDidMount?”

  1. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time
    a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that service?

  2. Does your website have a contact page? I’m having trouble locating it but, I’d like to shoot you an email.
    I’ve got some recommendations for your blog you might be interested in hearing.

    Either way, great blog and I look forward to seeing it improve over

  3. Hello! This is kind of off topic but I need some advice from an established blog.
    Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.

    I’m thinking about creating my own but I’m not sure where to start.
    Do you have any ideas or suggestions? Cheers

  4. Greetings from Los angeles! I’m bored to tears at work so I decided to browse your blog on my iphone during lunch break.
    I enjoy the information you present here and can’t wait to take a look when I get home.
    I’m surprised at how quick your blog loaded on my
    phone .. I’m not even using WIFI, just 3G .. Anyways, superb blog!

  5. Hey there, I think your blog might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a quick heads up!
    Other then that, wonderful blog!

  6. You really make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand.
    It seems too complex and very broad for me. I’m looking forward for your next post, I’ll
    try to get the hang of it!

  7. Thanks for every other great post. The place
    else may anyone get that type of info in such an ideal manner of writing?
    I have a presentation subsequent week, and I am on the search for
    such information.

  8. I’m really impressed with your writing skills and also with the
    layout on your weblog. Is this a paid theme or did
    you modify it yourself? Anyway keep up the nice quality
    writing, it’s rare to see a nice blog like this one these days.

  9. I’m extremely impressed along with your writing abilities and also with the format for your blog.
    Is that this a paid subject or did you customize it your self?

    Anyway stay up the nice high quality writing, it’s rare to peer a great weblog like this one

  10. Woah! I’m really loving the template/theme of this website.

    It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance”
    between superb usability and visual appearance.
    I must say you have done a amazing job with this. Additionally, the
    blog loads super fast for me on Internet explorer. Outstanding Blog!

  11. It’s a shame you don’t have a donate button! I’d definitely donate to this superb blog!
    I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will talk about this site with
    my Facebook group. Chat soon!

  12. Howdy! Quick question that’s entirely off topic.

    Do you know how to make your site mobile friendly? My site looks weird when viewing from my apple iphone.
    I’m trying to find a theme or plugin that might be able to fix this problem.
    If you have any recommendations, please share. Thanks!

  13. Nice blog here! Also your site lots up very fast!

    What web host are you the usage of? Can I am getting your affiliate link in your host?
    I desire my website loaded up as quickly as yours lol

  14. Excellent goods from you, man. I have understand your stuff previous to and you’re just too wonderful.
    I actually like what you’ve acquired here, certainly like what you are saying
    and the way in which you say it. You make it enjoyable and you still care for to keep it wise.
    I can not wait to read far more from you. This is really a great web site.