React.js Flashcards
What is React?
a JavaScript library for creating user interfaces
What is a React element?
object
How do you mount a React element to the DOM?
ReactDOM.render()
What is JSX?
syntax extension to JavaScript (JS plus some more).
-Transpilation step where JSX gets transpiled into JS
Why must the React object be imported when authoring JSX in a module?
In order to use the methods on the react object (ex. react.createElement)
How can you make Webpack and Babel work together to convert JSX into valid JavaScript?
Use babel loader and the plugin @babel/plugin-transform-react-jsx
What is a React component?
a JS function that accepts props as inputs and returns react elements
How do you define a function component in React?
function definition or a class, capitalize the names
How do you mount a component to the DOM?
ReactDOM.render()
What are props in React?
- Props is the shorthand for Properties in React.
- Props are read-only components which must be kept pure i.e. immutable.
- Props are always passed down from the parent to the child components throughout the application.
- A child component can never send a prop back to the parent component. This helps in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.
How do you pass props to a component?
as a key=”value” pair inside react element
How do you write JavaScript expressions in JSX?
{ }
What Array method is commonly used to create a list of React elements?
array.prototype.map
What is the best value to use as a “key” prop when rendering lists?
ID number or name from data that distinguishes it from another piece of sibling data
How do you create “class” component in React?
class ComponentName extends React.Component { render() { } }
How do you access props in a class component?
this.props
What is the purpose of state in React?
- States are the source of data and must be kept as simple as possible.
- States are the objects which determine components rendering and behavior.
- States are mutable unlike the props and create dynamic and interactive components.
- States are accessed via this.state().
How to you pass an event handler to a React element?
attached as a prop to your react element returned from the render method
What are controlled components?
form input whose value is controlled by React
What two props must you pass to an input for it to be “controlled”?
value and onChange
When does React call a component’s componentDidMount method?
Right when component is mounted in ReactDOM.render
Name three React.Component lifecycle methods.
componentDidMount
componentWillUnmount
componentDidUpdate
How do you pass data to a child component?
by passing the data through the props when rendering/returning the children react element
Basic Project Structure for a React Project
1) A src folder containing all JS modules
2) Index.html
3) Package.json
4) Module packager or build tool