React Flashcards
React is breaking code apart into pieces called…
components
What are the 5 steps of Thinking in React?
- Break the UI in to a component hierarchy
- Build a static version in React
- Find the minimal but complete representation of UI state
- Identify where your state should live
Break the UI in to a component hierarchy. What are the three different ways to do so?
- Programming. Technique for deciding if you should create a new function or object. You can follow the Single Responsibility principle, where a component should ideally do only one thing
- CSS. Consider what would you make class selectors for
- Design. Think how you would organize the design’s layers
Find the minimal but complete representation of UI state. What questions would you ask?
- Does it remain unchanged over time? If so, it isn’t state.
- Is it passed in from a parent via props? If so, it isn’t state.
- Can you compute it based on existing state or props in your component? If so, it definitely isn’t state!
What’s left is probably state.
What is a component in React?
Piece of reusable code that represents a part of a user interface
export default function Square() {}
What does ‘export’ and ‘default’ mean?
export means that this function can be accessible form other files as well.
default means that this function is the main function in that file
React components need to return a single JSX element. If you have for example three different components, what do you do?
You wrap them inside <div></div> or <></>
React provides a special function called useState. What can you do with it?
you can call from your component to let it “remember” things
What are props?
Props are information that you pass to a JSX tag
How do you tell difference between React component and HTML tags?
React components start with the capital letter whilst HTML is lowercase
In what do you wrap around JS variable so you could use it in JSX?
Curly braces {}
style={{}} is not special syntax in JSX. What is it?
It is an object inside JSX curly braces
What JS features do you usually rely on when you want to render lists in React?
For loop or map() function
If you have a list of items in React, what should you give each and every item?
Key attribute. It gives each element an unique identification which helps to find that items among its siblings
In React where should the key attribute come from?
It should be coming from your data, such as database ID
In React what shall you do if you want your components to remember some information and display it?
useState
const [count, setCount] = useState(0);
Explain this.
You’ll get two things from useState, first parameter which is the current state and second parameter which allows you to change the current state. Right now the state is 0 because that’s what we passed there.
But if we want to change the count, we use setCount for that
Syntax is array destructing
In React what are called Hooks?
Functions starting with use. For example useState is a built-in hook
In React how can you change data between components? (for example you want buttons to update the count together whenever anyone has pressed one of the buttons)
You put useState on top of the closest component which contains the buttons
What is markup? Name some markup languages
Set of tags assigned to elements which declare how elements should be displayed or how they relate to one another. JSX, HTML, XML
React components are regular JavaScript functions except: (2)
- React functions start with capital letter
- React component returns JSX markup
Why is React good in that sense that without it CSS, HTML and JS would all be in different files
It helps you put logic, rendering and markup in the same file so everything that considers for example a Button is all found in one place. And it helps to also isolate details that are unrelated (so that you wouldn’t have button and navbar markup in the same file or sth like that)
What are the rules of JSX? (3)
- Return a single root element. To return multiple elements wrap them with a single parent tag
- Close all tags
- camelCase most of the things
Where to use curly braces in JSX? (2)
- As text directly inside JSX tag
- As attributes immediately following the ‘=’ sign
In React with what you need to wrap props when passing them to another functions?
Curly braces {}
In React how can you specify a default value on prop? When is the default value being used?
Right after the parameter put ‘=’ and then the value.
Default value is being used if the prop is missing or assigned as undefined
When you nest content in JSX content, in what prop the parent component will receive that information?
In prop called ‘children’
In React, there are three ways how you can conditionally render JSX using JS syntax. What are they?
- If statements
- &&
- ? and :
What to use when you want components to “remember” things?
useState Hook
What are event handlers?
They’re functions which are triggered when interaction happens such as clicking, hovering, focusing form inputs etc.