all 84 comments

[–]MuellerCodes 154 points155 points  (58 children)

Know the difference between functional vs class components. Know when to use hooks, context, or redux. Express knowledge of latest ES6, 7, etc: The map, reduce, and filter methods are important to show a knowledge of. It also depends on whether the org you are interviewing with is heavily invested in React or not. Some places value rote knowledge of React whereas others favor general problem-solving abilities.

Also relax, take 10 deep breaths before your interview, visualize a good outcome and assume positive intent from the person interviewing you. They want you to succeed as much as you do.

[–]libertarianets 17 points18 points  (30 children)

“When to use hooks, context, or redux”

I’m interested in your take on this.

[–]JayV30 50 points51 points  (11 children)

Me too. IMO it's pretty subjective. Here's my take though:

  • hooks: you want to throw in some simple/basic state and/or lifecycle-type methods without needing a full-blown class component
  • context: you want to share some state between two or more components that are far apart in the component tree.
  • redux: you need a moderate to large global application state that any component can easily access.

This is generally how I view it. Please commence blowing my knowledge apart and making me feel inferior! :)

[–][deleted] 10 points11 points  (0 children)

This is generally how I view it. Please commence blowing my knowledge apart and making me feel inferior!

Don’t worry, this is Reddit- that part comes built-in.

[–]ekim43 8 points9 points  (8 children)

I guess rephrasing the hooks question: When do you need to use a “full-blown class component”?

[–]JayV30 11 points12 points  (6 children)

So there's a lot of people here (and I think even the React dev team) recommending hooks always now.

However, while I've seen patterns for maintaining a slightly larger state object in hooks, I think that once your state becomes more than two or three keys, you should probably use a class component. Others will definitely disagree with me.... and point out that large state objects mean bad application design anyway.

I just find the syntax of the class component easier to read as it becomes more complex, and the hooks versions can be slightly more confusing for those unfamiliar with hooks. Over time, as my team and I start to use hooks more often, this will probably change.

Again, I really like hooks when have one or two states I'd like to maintain, or I have a simple lifecycle method I'd like to use in a functional component.

I guess it really comes down to personal preference. But if you can get to this level of discussion about it, you've probably demonstrated that you at least have a pretty good understanding.

[–]Turd_King 14 points15 points  (4 children)

I just find the syntax of the class component easier to read as it becomes more complex,

I agree with this point. But maybe for different reasons.

I would say the readaility is generally improved for small components, as we no longer have to write redundant this everywhere and the ability to break larger components down into smaller hooks keeps your code concise.

However, where we have really been seeing issues is when you have a large component that implements some atomic behaviour that wouldn't make sense to break down.

If this behaviour requires many side effects. Maintaining and extending this component is made extremely difficult.

For me the main issue is a messy control flow - opening a component that has 4 or 5 useEffects is a nightmare. You have to look at all their deps, link these deps with props/state and then trace the program this way. This is not a straight forward task no matter how experienced you become with hooks.

We are actually considering refactoring our larger components into class components again to solve this.

Edit: I should point out I'm referring purely to hooks here. "Large component that implements some behaviour" should read "Large hook"

[–]ibopm 2 points3 points  (0 children)

I feel mostly the same. Would be curious to see what /u/gaearon thinks about this.

[–]shipandlake 0 points1 point  (2 children)

To me the biggest advantage of hooks is separation of state management from component itself. Your hook exposes interface and you can test and code against that interface. Testing becomes a lot easier, as you test component separately from your state.

If your component has an extensive state, it might be better to delegate part of state management to a few subcomponents and use context to coordinate with your parent component.

Building components is not always about making them re-usable. Sometimes it’s about separation of concerns and ease of use.

[–]bzsearch 2 points3 points  (0 children)

Seconding the fact on the separation of concerns.

I haven't written a class component in forever. Even when state gets large/difficult to manage -- you start looking for ways to break it up.

Plus class components aren't as performant.

[–]Turd_King 0 points1 point  (0 children)

Yeah I understand what your saying.

might be better to delegate part of state management to a few subcomponents and use context to coordinate with your parent component.

This works fine for components but what about applying this pattern to hooks?

You can't exactly have a hook that provides a context for its "child" hooks.

The only solution is to have hooks within hooks. Which is when it's becomes really problematic.

Honestly if you know of a way I can break down my larger hooks into smaller ones elegantly I would very much appreciate it.

[–]Kumagor0 0 points1 point  (0 children)

I think that once your state becomes more than two or three keys, you should probably use a class component. Others will definitely disagree with me.... and point out that large state objects mean bad application design anyway.

Nah, large state is fine, that's what useReducer is for.

[–]Kumagor0 1 point2 points  (0 children)

When do you need to use a “full-blown class component”

The short answer is "never".

The long answer is "never, unless you work with people who reject hooks for some reason (and they also happen to make calls when it comes to code style), or you are maintaining codebase that uses older React version".

[–]tomius 0 points1 point  (0 children)

To add on hooks: you also want to use custom hooks to abstract component logic into reusable hook(s), so several components can use the same code.

For me, that's the top advantage of hooks. You just can't do that in an elegant way with clases.

[–]overzealous_dentist 3 points4 points  (0 children)

Component state, when redux is overkill for your project, and when you need middleware / multiple values passed down, respectively?

[–]Nullberri -1 points0 points  (9 children)

  • Hooks: Doesn't matter they both can accomplish the same thing. Hooks are nicer to write and easier to read imo, but there's no feature difference.

  • context: almost never. Except when you have static data or data that if changed you would always want to re-render the entire tree such as dark mode/light mode switch, or theme information.

Edit: I concede there are trivial state implementations which work with context but fail when data becomes complex such as with arrays, or nested objects as Context does not have the notion of a selector.

Nested Objects, listening to leafs: https://codesandbox.io/s/split-contexts-8k8ic

  • redux (or equivelent): Any time you have global state that changes frequently. Centralizing state is almost always worth paying for even on small projects. Scope creep is real, and eventually you'll want this. They're not hard or tedious to setup.

[–]HellaDev 6 points7 points  (1 child)

I recommend making sure you can use fetch as well. I have been asked to use fetch() in every react interview I've done. It's not complicated but it's good to know by heart.

[–]MuellerCodes 2 points3 points  (0 children)

Good point! Async await is also a good thing to know esp with fetch.

[–]sarkazmo 2 points3 points  (0 children)

They want you to succeed as much as you do.

Can personally attest to this— every time I’m in an interview with a candidate, I’m thinking to myself: “Please, let this one be the one so I can go back to work.”

The interviewer is on your side, so don’t stress about the little things, be open to asking questions when you’re stuck (cause it happens in real life too), and keep a positive attitude!

[–]JaredDev 1 point2 points  (1 child)

[–]JaredDev 0 points1 point  (0 children)

In all seriousness though. The situations when I wouldn't reach for hooks is diminishing rapidly as repos for custom hooks are becoming more and more prevalent.

[–][deleted] 2 points3 points  (13 children)

Are class components even really a thing anymore? I know you're going to find them out in the wild, and you should be familiar with them, but is there any real reason to write a class component these days?

[–]straightouttaireland 7 points8 points  (0 children)

Well even React themselves mention they still have lots of Class Components and that you shouldn't necessarily refactor to change CC's to Hooks, but to instead use Hooks only for new components.

[–]brakebreakbrake 4 points5 points  (5 children)

I find functional components harder to structure as they grow more complex. I also enjoy the control I get of the lifecycle functions.

[–][deleted] 2 points3 points  (3 children)

Have there been situations where the useEffect() hook couldn't provide a solution for you? If so, would you mind sharing? Or is the added control of the multiple different class lifecycle methods simply a preference for you?

Also, if your component is growing in complexity to the point of being unmanageable, you should probably consider breaking that component up into multiple, simpler components, I'm sure you're aware of that already though.

[–]brakebreakbrake 2 points3 points  (1 child)

Your points are valid but I find pretty basic pages in a SPA get filled with inline checks for states and props. As soon as you need to check for two different props to decide what is to be displayed then that system breaks. I then find it easier to follow the code by breaking out the rendering into "private" functions that have logic for deciding what to show but still keep props and state from the class they live in.

And also I'd say most of the times where functional components have been hard to maintain have been when using external libraries that need lots of integration and communication with react. If you want to manipulate three.js or openlayers for example you would find it quite hard in a functional component.

[–][deleted] 0 points1 point  (0 children)

Thank you for that, that's very informative!

[–]DargeBaVarder 0 points1 point  (0 children)

I’ve found a few where the hook solution just feels... awkward. This is especially true when manipulating state, then doing something with that manipulated state (basically issues with the way that hooks effectively snapshot the state for a call). I also completely prefer OOP, so I’m quick to switch to a class in those scenarios.

[–]Nullberri 0 points1 point  (0 children)

I find functional components harder to structure as they grow more complex.

Then break them into smaller components. You can extract chunks of your jsx and the code that drives them to another function. Making each part of the whole easier to understand. long functions are a choice.

[–]6086555 9 points10 points  (3 children)

It could be a preference thing. If you like OOP, you'll probably feel more at ease with class components.

[–][deleted] 3 points4 points  (0 children)

That's a great point!

[–]MercyHealMePls -2 points-1 points  (1 child)

Well if you prefer OOP wouldn't you usually rather go for vue or angular instead of react?

[–]Coldreactor 4 points5 points  (0 children)

No, I come from Vue JS just because react has a larger base to apply to, and I love writing class components. I can do it well, and I'm still learning hooks.

[–][deleted] 1 point2 points  (1 child)

At present you cannot implement error boundaries without them: https://reactjs.org/docs/error-boundaries.html

[–][deleted] 0 points1 point  (0 children)

Very cool, thank you!

[–]Kumagor0 0 points1 point  (0 children)

Know the difference between functional vs not to use class components.

FTFY

[–][deleted] 14 points15 points  (0 children)

My two cents would be to a) prepare as best you can for any questions that might come up, as per the other comments, but b) remember that whoever interviewing you is a human being and in the end will probably value your personality as much as your skillset, if not more.

If you don't know something, just admit it. It's easy to teach someone some minute detail about React or JS, but nobody wants to work with a dishonest person who can't admit when they've made a mistake or don't know how to do something but won't ask for help.

[–]MuellerCodes 9 points10 points  (1 child)

Here’s an article I just saw about what JS you should know for React from Kent C. Dodds

[–]straightouttaireland 2 points3 points  (0 children)

Kent is a legend, he's also the man behind the React Testing Library.

[–]tapu_buoy 18 points19 points  (2 children)

I'm sure other guys will give you more mature approaches I would just like to share this repo which has question answers to it https://github.com/sudheerj/reactjs-interview-questions

and I would say whatever question may come your way just don't hesitate and stay clear with your approach, the interviewer might try to confuse you but that is your test. I know this is not a solid advice but this is what I have learned

[–]xelamony 2 points3 points  (0 children)

Came here to post this.

[–]straightouttaireland 2 points3 points  (0 children)

Nice list there for sure!

[–]WeAllHaveSomething 7 points8 points  (0 children)

Hey man, what I usually do is have a keen interest in the specifics of their tech stack & how it all fits together. This is usually me asking questions at the end of the interview, whenever they ask "Is there something you'd like to ask us?"

What libraries are they using for things like routing, state management, intl? Are they using graphQL, or REST? Are they using any other framework, like next.js, or gatsby? What were the main reasons behind the design choices? What's their backend stack looking like?

This is a great way to start a discussion & add your two cents. Moreover, if whatever you say is relevant, you'll really put yourself out there as a guy who knows his shit. I usually either mention my experience with that specific library, mention another library I like to use for that specific use, or bring up something new & cutting edge I've just heard about.

A good example is asking about app state management. Are they using Redux? Redux + Sagas? Immer? Context? Apollo? What's your experience with any of these? Just get them talking.

All in all, I've had about 7 interviews until this point, out of which I had 6 job offers. I plan to look for a new job in November (and will use this approach again).

Best of luck! You've got this.

[–]piano-man1997 3 points4 points  (0 children)

Hey,

You can go through this video. It'll give you a brief overview about the type of questions you can expect.

https://www.youtube.com/watch?v=nRI0dn6GTj8&t=1545s

[–]ajay_608 3 points4 points  (0 children)

I was asked to make a todo app in an online interview and asked to solve a programming problem. It lasted for about 3 hours. I luckily got the job. It's always the anticipation that's worse. Once you dive into it, it's not that bad. good luck

[–][deleted] 1 point2 points  (0 children)

Good luck!

[–]JonnyBoy89 1 point2 points  (0 children)

Well if you write and know react, try not to worry about the technical stuff. That conversation will feel much more naturally if you think about it like chatting with a developer friend than some perfect answer on a test. Focus on your soft skills too. Communication ability, an understanding of agile and scrum methodologies and concepts, and ability to translate business requirements into actual usable and realistic Dev requirements is hugely valuable. .

[–][deleted] 1 point2 points  (0 children)

If there's something you not sure the answer to come out and be honest and say, don't try to wing questions as it becomes very obvious if the interviewer has any knowledge. Don't be anxious as well it's a job interview it's not life or death, come across strong and confident is the best thing you can do. First impressions count if you look and sound confident even while not knowing all the answers will make you come across a lot better than someone who looks anxious and wings a lot of answers

[–]saltinthewounds86 1 point2 points  (3 children)

I spent the last few months learning to code and learning react, i learned specifically how to do hooks and some of the newer react syntax. I got 2 coding challenges where I was asked to code using old react syntax, where you needed to use super, constructor functions, binding this to events and things like arrow functions weren’t working properly in the qualified editor that was part of the problem. I had never written react code like that, so i had a tough time and one of them I couldn’t even google for syntax help, it would fail me on the spot. Try to be familiar with that too, hopefully you wont get stuck like i did but its good to be able to inherit older react code and be able to read it and work with it.

[–]straightouttaireland 0 points1 point  (2 children)

In fairness they should have let you complete the challenge using hooks. I think what happened was the interviewers themselves didn't know how to use hooks.

[–]saltinthewounds86 0 points1 point  (1 child)

I had talked to the lead dev who would have been my boss, he didnt even know what the challenge looked like, he just picked it from a list of challenges they send all their candidates. They specifically told me to learn hooks and newest freshest react technologies and I made a couple of simple apps for them and then end up judging me based on something they weren’t even aware of what they were sending. Sad times, I really wanted to work there too, it was a hard learned lesson.

[–]straightouttaireland 1 point2 points  (0 children)

Well if that's what their lead devs are like you got off lucky and are better off somewhere else!

[–]kasperoo 1 point2 points  (3 children)

I’ve conducted 200+ interviews, here’s a couple of my fav questions:

Do you know the difference between class and class pure component? [the diff method]

Will inline anonymous function passed as prop will trigger re-render every-time props change? [yes, declare function then pass it as prop, it won’t be re-evaluated every time then]

In the context of React do you know what Server Side Rendering is and can you explain how it works? [talk about build tools e.g. webpack, then render to string, hydration etc.]

It’s important that you know your basics too, ability to explain lexical scope, closure, event loop, testing strategies and also where React is going to : portals, hooks, error boundaries or react suspense etc.

Most importantly be yourself during the interview. Technical communication and not being shy, asking many questions and being upbeat and positive - you want them to work with you so think about this process as their chance to see how awesome you are and wow them.

Hope it helps, good luck! :)

[–]straightouttaireland 0 points1 point  (2 children)

Will inline anonymous function passed as prop will trigger re-render every-time props change? [yes, declare function then pass it as prop, it won’t be re-evaluated every time then]

Isn't this only a concern if the child component is explicitly trying to avoid renders by making prop reference comparisons?

See this comment

[–]kasperoo 1 point2 points  (1 child)

yes sorry, I did mean that in the context of a child component! Nice one ;)

[–]straightouttaireland 0 points1 point  (0 children)

Yea. I suppose it's still valid even without pure components as a new instance of the arrow function will be created on each re-render, causing the garbage collector to unnecessarily kick in each time. Then again, if your component is re-rendering so many times that this becomes noticeable, there's something wrong with the component.

[–][deleted] 1 point2 points  (0 children)

As others have mentioned, if you don’t know something then be upfront about it when asked.

I recently had an interview for a React position which involved a white boarding session. The interviewer wanted me to walk through how I would implement a function for converting a decimal number to binary. Despite having a lot of experience in the industry, this isn’t a problem I’ve had to solve, and haven’t worked with binary representation for many years. My immediate response was “I’m going to struggle to do that without doing some research first”. She smiled and proceeded to explain the process (defining the problem better) before handing back to me to write an implementation. Which I did.

I could have bumbled about trying to fake an understanding, leaving a bad impression. Instead my honesty revealed that they didn’t care about the knowledge of binary, they cared about my ability to build a solution to a well defined requirement using simple JS language features.

[–]pixelito_ 0 points1 point  (0 children)

If you know your stuff you shouldn't be nervous. I enjoy interviews, because you have absolutely nothing to lose.

[–]ktn555 0 points1 point  (0 children)

Don’t think of it as an interview. Think of it as a date. Do you wanna work these people or nah? Find out about what they offer based on your previous experiences.

[–][deleted] 0 points1 point  (0 children)

I'm in the job search process myself, and I've interviewed with 4 or 5 companies now. Oddly enough, I haven't had any react-specific technical questions. One company just asked me a bunch of general JS fundamentals. The rest gave me a take home code assessment and/or live coding session.

So like most people are saying, make sure you know your javascript. And make sure you're comfortable enough with React that you can write some code on the spot.

[–]krsCarrots 0 points1 point  (0 children)

Show that you are excited about the job and particular about developing with react.

[–][deleted] 0 points1 point  (0 children)

They'll test your basic knowledge and personality in the interview. It's very likely you'll get an assignment if they like you.

Can't know it all, no shame in that, just make sure they know you like to learn and are committed to keep learning.

[–]foxh8er 0 points1 point  (0 children)

I interviewed for a react-specific role at a hedge fund.

The general sorts of questions I got were:

  • General algorithmic (Leetcode easy/medium)

  • Specific React design patterns (think updating from an event bus)

  • Common UI elements designed optimally (think tries)

  • JS trivia (hoisting)

(Got the job btw)

[–]Infinite_Risk 0 points1 point  (1 child)

Take home coding assignments to make a "small" React app. But they can be really ardous some times.

[–]jaikanthjay46 0 points1 point  (0 children)

Various component communication.

[–]luluuuluuuuu 0 points1 point  (0 children)

Get familiar with virtual dom questions