ReactJS - beginner Flashcards
How do you declare a new React component class?
You sub class React.Component
class MyComponentClass extends React.Component { render() { return <h1>Hello world</h1>; } }
What is the convention for writing Javascript class names ?
Upper Camel Case
for example: MyComponentClass
How can you tell the difference between JSX that is HTML-like and JSX that is a component instance?
Capitalisation.
< MyComponentClass /> // component instance
<h1>Hello there</h1>
// HTML-like JSX
In a JSX element, a capitalised first letter says, “I will be a component instance and not an HTML tag.”
How do you call a components render method?
You pass the component to ReactDOM.render(). ReactDOM.render calls the render method of MyComponentClass for you.
ReactDOM.render(
< MyComponentClass />,
document.getElementById(‘app’)
);
What are “named exports”?
Named exports are variables that you export using the export key word like this :
// file.js export const name='Tom';
And then import them by their name within curly brackets like this:
// otherfile.js import { name } from 'file.js';
What is a default export?
This is when a variable is exported from a file using the “default” keyword like this:
// file.js const name="Tom"; export default name;
As this will be the default export when you import it you do not need to specify its name and can give the variable it will be placed into any name you want. The curly braces are not required for a default import.
// otherfile.js import someName from 'file.js';
someName will now contain “Tom”;
What is this ?
< MyComponentClass />
An instance of a React component.
How do you view a components props?
You use this.props within the components class definition.
class PropsDisplayer extends React.Component {
render() {
const stringProps = JSON.stringify(this.props);
return (
<div>
<h1>CHECK OUT MY PROPS OBJECT</h1>
<h2>{stringProps}</h2>
</div>
);
}
}
What does JSON.stringify() do?
It turns a JavaScript object or value into a JSON string.
What does JSON.parse() do?
It turns a JSON string into a corresponding JavaScript variable.
Can a web browser read a Javascript file that contains JSX?
No a web browser can not read JSX. The JavaScript file will need to be compiled first with a JSX compiler. This will translate any JSX into regular JavaScript.
How can you write JSX code without using JSX?
Use React.createElement()
const h1 = <h1>Hello World</h1>;
// can be rewritten without JSX like this
const h1 = React.createElement( "h1", null, "Hello World" );
Can JSX elements have attributes ?
Yes they can have attributes just like HTML elements.
Can you access variables declared outside of a JSX expression in a JSX expression?
Yes you can.
// Declare a variable const name = "Tom";
// Access your variable from inside a JSX expression const greeting = <p>Hello {name}!</p>;
Can you nest JSX elements?
Yes JSX elements can be nested just like HTML elements.
Can you use an if statement in a JSX expression?
No you cannot.