React Flashcards
Why React?
Fast
Extensive library
Active community
Compositional/Modular
Declarative
Unidirectional Dataflow
just javascript
What is a simple server we can setup for development
npm i -g live-server
live-server public
What is Babel?
Javascript compiler
Compiles jsx into es5
Why can if statements not be used in templates but a ternary can?
Because a ternary is an expression while if statements is a statement or controll structure. ONLY expressions can be used in jsx.
What are 3 things that are ignored by jsx?
undefined, null and booleans
How to add conditional logic to jsx?
- Ternary expressions
- Logical && operator
- Logical || operator
- Define a function outside of the template that handles conditional logic and then call the funciton in jsx
Difference between var, let, const.
Var can be redefined and reassigned.
let cannot be redefined but can be reassigned
const cannot be redefined or reassigned.
let and const are block scoped, whereas var is excecution context or function scoped. Var variables are hoisted, whereas const and let are not.
What needs to happen before the browser can read jsx?
The jsx code or file needs to be transpiled in javascript. This is done through babel.
difference between arrow functions and function declarations and expressions
Arrow functions are anonymous
arrow expression Syntax is more concise, do not need to explicitly return a value when declared on the same line
Arrow funcitons does not bind it’s own this value. It uses it’s parents this value, If you want to use an objects this value, use function declarations, if you want to use an object’s parents this value then use arrow function.
arguments objects are not bound with arrow functions, which means you cannot access the arguments object within the arrow funcitons
What needs to be done for a es5 function to use it’s parent’s this value
const that = this
function(){ that.value }
How are the attributes in jsx different from html attributes
class = className
use camelCase instead of hyphens
form-target = formTarget
JSX does not have built in data binding, what does this mean?
It meas that whenever a value in a template is updated the template has to be re-rendered in order for it to be updated.
What is data-binding?
Keeping state in sync with views
How to turn a value into a boolean
!!value
How to turn a value into a boolean
return !!value
In order to create a component what must an es6 class extend?
React.component
What is the one method tha t must be defined in a React componenent class?
render(){
return (
)
}
How to get a value from the input field
add name=”someName” attribute to the input field
in the event handler get the value with:
e.target.elements.someName.value
how to bind a method in a class component
//in constructor this.methodName = this.methodName.bind(this)
How to set default state in a component
//in constructor this.state = { key:value }
How to change or update state?
this.setState((prevState)=>{ return { key: prevState.value + 1 } })
What is the difference between a class based component and a stateless functional component?
SFC’s are just functions that return jsx. SFC’s can acces props but cannot contain states.
SFC’s are a lot faster than class based components and should therefore be used instead of class based components whenever possible?
Lifecycle methods can be added to class based components but not SFC’s
How to add default props to a component?
Header.defaultProps = {
key:’some value’
}
LIfecycle of a component
- coponentWillMount -executes before initial rendering
- componentDidMount - imediately after the initial rendering
- componentWillRecieveProps - When component recieves new props
- shouldComponentUpdate -before rendering, after receiving new props or state
- componentWillUpdate -after shouldComponentUpdate returns true
- componentDidUpdate - after rerender
- .componentWillUnmount -just before removing component from Dom
Info
The info is: {props.info}