React Flashcards
Why does React need a “root” element?
-React is a framework that is fully made of JavaScript and no HTML. So it needs an html element where it can render out its own DOM tree and hook into
Was ist JSX and is it HTML?
-No, that is a domain specific language that mimics HTML but sits on top of JavaScript. Babel etc will transfer JSX into JavaScript later on. The HTML page will be different then what is in JSX.
What is the difference between state and props?
- In a state a component can store their internal state for example like a variable used within the component to toggle a modal. Or informations about a checkbox toggle whis is data that only the component itself must know not the whole application.
- The props are beeing used when passing down information from a parent component to its child through prop drilling. That is the main way data is handled in React.
What is context?
-Context is a global available prop that the whole application and every component can use, for example like translating texts. Often used only for specific usecases.
What are proptyped and what are the cons and pros?
-With proptypes you make a component know and enforce which data types it expects. You can determine what a component needs to work with it. But they can become a problem because often they are documented lazily or used with legacy calls like .objec tinstead of .shapeOf. So you also have to maintain a lot more code.
Which Lifecycle events are the most common one from your perspective?
- ComponentWillMount
- ComponentWillUpdate
- ComponentWillReceiveProps when component is getting new state but is NOT rerendering
When do you use a pure component, when do you use a class?
- Pure componentes are used for optimizing performance
- Pure components have less overhead when you don’t need a state or lifecycle events
Explain how the React rendering works in your terms!
-React listens for DOM updates and rerenders the DOM tree on every change but it performs way faster because it can determine if certain components states have changed. So it only needs to rerender parts of the Tree when they really have changed.
What is Redux?
- Redux is a popular solution for storing state in React in a global manner.
- Data is stored in an global object and actions can be used to make changes to it.
- Components can be connected if needed to access the global application state.
How does Redux work?
-You can use reducers that listens to actions and interact with a store/state. Then for actions there are action creators which can dispatch actions and transfer data in the redux store. React will rerender the dom with the changes to the state/store.
When do you use Redux?
- When an application needs a global state which is shared between many components. Only the components needing that state are getting connected to Redux store.
- When an application does not have a shared store you need no Redux.
What is a container component?
- Seperate JSX away from the logic of the component
- Only used when a component is doing more than showing stuff or passing data through
What is a view component?
- A component that only display passed through data to JSX
- A view component is seperated from the logic component but still often not resuable
What components make for a good container candidate?
- Container should be used on components that are more complex because the container adds complexity itself.
- Adding a container to a small component will make it more complex than itself is
What is a controlled component?
A controlled component takes it data through props and is controlled by parent components. It notifies changes on callbacks with onChange. It is a dumb component.
What is a uncontrolled component?
Stores state itself internally and the DOM is queried using the ref to find values like in HTML.
What is the virtual DOM?
- A node tree containing elements used by React. The render() method creates the node tree from components and update the tree after actions/mutations.
- The virtual DOM is diff compared against the previous DOM and only updates the DOM where it really changed
Why use Redux?
- Avoiding Prop drilling which can get messy
- Having a global reachable data storage/state for the application
- Often used for API call data or data that is needed int he whole applicaiton
What happens when an action is called?
-Actions are contained in action creator methods
-When such a metod is invoked first the logic of the function will be sequentielly stepped though
-Then when condition are beeing triggered a dispatch can be triggered.
-This dispatch is registered by a reducer that listens to the action
-The reducer has contains conditional logic and a state to manipulate data or persist data coming from the action as a payload
The data is then available to connected components through props for all connected components
What are benefits of React?
- UI testing is easier
- Faster Application with single page
- JSX full JavaScript
- Virtual DOM
- Server Side and Client Side