KCD - Epic React / Articles Flashcards
What is a nullish coalescing operator?
“<div>Sometimes you want to fall back to a default value if a value is null or undefined.</div><div><br></br></div><div>This would look like this:</div><div><img></img><br></br></div><div><br></br></div><div>But that was problematic for numbers or booleans where ““0”” or ““false”” could be valid values. So then we used to have to do this:</div><div><img></img><br></br></div><div><br></br></div><div>Now, we have a double question mark:</div><div><img></img><br></br></div><div><br></br></div><div>This returns the right side whenever the variable on the left side is null or undefined (not just falsey) - if the left side is 0 or falsey, it returns that instead.</div>”
How do you append directly to the page (instead of an element on the page)
“<div><img></img><br></br></div><div><span></span></div>”
What is special about the children prop with React.createElement?
It is a special prop where if you have one element, that’s fine, but if you want multiple elements, you can either just keep adding arguments to createElement and they all get slurped up and thrown into children, or you can provide it as an array - either way it converts it into an array. (This will be useful when creating custom components)<br></br><br></br><br></br>
Why do we have to use Babel?
Because JSX is not actually Javascript, so Babel is the code compiler that converts it into React.
What two things from JavaScript cannot go inside JSX curly braces?
You can’t do if/else conditionals, and you can’t define variables - that would have to happen above in Javascript land.
What is a component, really?
Basically just a JS function which returns something that is renderable (more React elements, strings, null, numbers, etc.)
In order for JSX to interpret a function between angle brackets as a component, what is allowed?
- The function starts with a capital letter<div>2. OR a lower or uppercase with a dot proprty</div><div>3. OR an Upper_Snake_Case name</div>
What is the purpose of propTypes?
They give an error when we passed the wrong value (or no value) to a component.
What happens if I pass the wrong kind of value into a prop typed component while in production?
Nothing. Prop Types add runtimee overhead, so they aren’t run in production.
How do you implement PropTypes?
“It’s a package made available through React. To use it, imagine a component named Message that requires it’s two props to be strings and to both be passed.<div><br></br></div><div><img></img><br></br></div>”
How is the style prop different between HTML and React?
“In HTML you pass a string of CSS, while in React you pass an object of CSS.<div><img></img><br></br></div>”
Where does your form’s onSubmit function go?
On the form element itself, not on the submit button.
What do you need to add to an input and a label to associate them together?
The label needs an htmlFor= tag that matches the input’s id.
What does the browser do by default when a form is submitted?
It makes a GET request to the current URL with the values of the form as query parameters in the URL, which then causes a full page refresh. (only named values are submitted)
What is a SyntheticEvent?
Not a real event from the browser, but an object that React creates for us that looks and behaves exactly like a regular event. You likely won’t know the difference - they do this for performance reasons, and for event delegation.
How can you get the value of an individual input on a form?
event.target[0].value - where the index of the array will depend on which input you are trying to get it from.
What should you do instead of accessing inputs by targeting the order in which they appear on the form and why?
Instead, you should name your inputs, and then whatever you have given as the name or id is accessible by the form as form.name, you should do this because it is much more change resillient (if the order changes, you’re still ok.)<div><br></br></div><div><br></br></div>
Why is it better to give all form inputs an id instead of a name?
Either will associate the input as a call-able item on the form, but screen readers need the label to be associate to the input, and in addition, if your labels aren’t tied to inputs through their htmlFor matching the input’s id, then when you click the label, it won’t go to the input, which usually you do want to happen.
What is a ref?
A ref is an object that stays consistent between renders of your React component.
What is the value on a ref that can be updated to any value at any time?
ref.current
Say you have an inputRef object created - how could you access the value?
inputRef.current.value
Describe useRef in terms of a box.
it’s like a box that can hold a mutable value in its .current value.<div><br></br></div>
What happens if you pass a ref object to React (like <div></div>
React will set its .current property to the corresponding DOM node whenever that node changes.
How is useRef helpful beyond just the ref attribute on an input?
useRef is handy for keeping any mutable values around, similar to an instance field in a class.
- Event handlers
- Asynchronous code (e.g.
setTimeout
orrequestAnimationFrame
callbacks) - Server side rendering
- Errors thrown in the error boundary itself (rather than its children)
componentDidCatch() is used to log error information.
So, if you need to catch an error on an event handler, you should just use the regular JS try / catch.
This component will then be supplied with the error as well as a resetErrorBoundary function which can be used to reset the ..error boundary (and clear the error from state so that it can be tried again)
- They can break when you refactor application code. False negatives
- They may not fail when you break application code. False positives
- What part of your untested codebase would be really bad if it broke? (The checkout process)
- Try to narrow it down to a unit or a few units of code (When clicking the ""checkout"" button a request with the cart items is sent to /checkout)
- Look at that code and consider who the ""users"" are (The developer rendering the checkout form, the end user clicking on the button)
- Write down a list of instructions for that user to manually test that code to make sure it's not broken. (render the form with some fake data in the cart, click the checkout button, ensure the mocked /checkout API was called with the right data, respond with a fake successful response, make sure the success message is displayed).
- Turn that list of instructions into an automated test.
findBy*
queries return a promise which resolves when an element is found which matches the given query. The promise is rejected if no element is found or if more than one element is found after a default timeout of 1000
ms."
const exampleInput = screen.getByLabelText(/example/i)