| Start |
|---|
| Props in JSX There are several different ways to specify props in JSX. JavaScript ExpressionsYou can pass any JavaScript expression as a prop, by surrounding it with Inside the If statements and for loops are not expressions in JavaScript, so they can't be used in JSX directly. String LiteralsOf course, you can just pass any When you pass a string literal, its value is HTML-unescaped. So these two JSX expressions are equivalent: This behavior is usually not relevant. It's only mentioned here for completeness. Props Default ValueIf you pass no value for a prop, it defaults to However, the React team says in their docs using this approach is not recommended, because it can be confused with the ES6 object shorthand Spread AttributesIf you already have props as an object, and you want to pass it in JSX, you can use |
| Children in JSX In JSX expressions that contain both an opening tag and a closing tag, the content between those tags is passed as a special prop: String LiteralsYou can put a string between the opening and closing tags and This is valid JSX, and Note that the HTML is unescaped, so you can generally write JSX just like you would write HTML. Bare in mind, that in this case JSX:
JSX ChildrenYou can provide more JSX elements as the children. This is useful for displaying nested components: You can mix together different types of children, so you can use string literals together with JSX children. This is another way in which JSX is like HTML, so that this is both valid JSX and valid HTML: Note that a React component can't return multiple React elements, but a single JSX expression can have multiple children. So if you want a component to render multiple things you can wrap them in a JavaScript ExpressionsYou can pass any JavaScript expression as children, by enclosing it within This is often useful for rendering a list of JSX expressions of arbitrary length. For example, this renders an HTML list: Note that JavaScript expressions can be mixed with other types of children. Functions as ChildrenNormally, JavaScript expressions inserted in JSX will evaluate to a string, a React element, or a list of those things. However, Children passed to a custom component can be anything, as long as that component transforms them into something React can understand before rendering. This usage is not common, but it works if you want to stretch what JSX is capable of. Ignored ValuesNote that This is extremely useful to conditionally render React elements. This JSX only renders a if One important caveat is that some "falsy" values, such as the One approach to fix this is to make sure that the expression before the Lastly, bare in mind that if you want a value like RemarksJSX is a preprocessor step that adds XML syntax to JavaScript. You can definitely use React without JSX but JSX makes React a lot more elegant. Just like XML, JSX tags have a tag name, attributes, and children. If an attribute value is enclosed in quotes, the value is a string. Otherwise, wrap the value in braces and the value is the enclosed JavaScript expression. Fundamentally, JSX just provides syntactic sugar for the So, the following JSX code: Compiles down to the following JavaScript code: In conclusion, note that the following line in JSX is neither a string nor HTML: It is called JSX, and it is a syntax extension to JavaScript. JSX may remind you of a template language, but it comes with the full power of JavaScript. The React team says in their docs that they recommend using it to describe what the UI should look like. |
Read Article
https://develop.horje.com/learn/1434/reference