Reputation: 3
I'm new to React and I'd like to understand how you can call a component inside another component. I have a component folder which contains a RecipeCard.js:
import React,{Component} from 'react';
import { Ingredients } from './Ingredients';
export class Recipe extends Component {
render() {
return <div class="recipecard">
<p class="texterecipecard">
Recette: {tartetomate.props.name}, <br/>
Difficulté: {tartetomate.props.difficulty},<br/>
Vous aurez besoin de {Ingredients}.<br/> {tartetomate.props.recette}
</p>
</div>;
}
};
const tartetomate = <Recipe name="Tarte tomate" difficulty="Moyenne" ingredients= {Ingredients} recette="Mélanger les ingrédients dans un moule, faire cuire le tout à 190 degrés pendant 20 min." />
When I try to call {Ingredients} inside the render part of my Recipe component, it does not work, there are no errors with eslint but it simply does not render. It leaves a blank. {Ingredients} refers to another component in a Ingredient.js file that simply renders a paragraph.
Why does this not work and how can I make it work?
Upvotes: 0
Views: 4368
Reputation: 6121
Ingredients
is a React component, which is a JS-class or JS-function.
These are not valid as a React child, and are not rendered, plus you should get a warning in the console, like:
Warning: Functions are not valid as a React child ...
You need to insert a React element instead of a React component), e.g.:
class Ingredients extends Component {
render(){
return <p>(content of Ingredients)</p>
}
}
export class Recipe extends Component {
render() {
return <div>
<p>
Vous aurez besoin de <Ingredients />.<br/>
</p>
</div>;
}
}
A React element is an Object, returned by e.g. React.createElement( Ingredients )
, or by using the equivalent JSX syntax <Ingredients />
You should not access your global tartetomate.props
from inside Recipe
. Instead use the props
of Recipe to pass the values, e.g.:
export class Recipe extends Component {
constructor(props) {
super(props); // assure this.props works correctly
}
render() {
return <div>
<p>
Recette: { this.props.name }, <br/>
Difficulté: { this.props.difficulty },<br/>
Vous aurez besoin de <Ingredients/>.<br/> { this.props.recette }
</p>
</div>;
}
}
Then you can add the Recipe
inside another component, like:
export class RecipeWithValues extends Component {
render(){
return <Recipe
name="Tarte tomate"
difficulty="Moyenne"
ingredients={ Ingredients }
recette="Mélanger les ingrédients dans un moule, faire cuire le tout à 190 degrés pendant 20 min."
/>;
}
}
tartetomate
Your tartetomate
is a React element already, created by the JSX syntax <Recipe ... />
, stored in the global scope. That looks like bad style, and you probably don't need it, because you can use <Recipe ... />
directly.
But if you need it for some reason, you could use it like:
const tartetomate = <Recipe
name="Tarte tomate"
difficulty="Moyenne"
ingredients={ Ingredients }
recette="Mélanger les ingrédients dans un moule, faire cuire le tout à 190 degrés pendant 20 min."
/>;
export class RecipeWithValues extends Component {
render(){
return <div>{ tartetomate }</div>;
}
}
Upvotes: 1
Reputation: 132
Try using React components like this :
import React from 'react';
function ComponentInside({name}) {
return (
<div>
Hello, {name}
</div>
)
}
function Component() {
return (
<div>
I'm the container
<ComponentInside name='Paul'/>
<ComponentInside name='Jack'/>
</div>
)
}
export default Component;
Then you can use <Component/>
in your App.js
Upvotes: 0