简体   繁体   中英

is it possible ? Calling a component inside the custom element in reach

enter image description here enter image description here

i want to change the body dynamcally how can i do this?

import React from 'react'
// import { Link } from 'react-router-dom'
export default function ProjectTemplate(props) {
    const Css= {
        "--background":`${props.isValue ? props.mode : 'red'}`
    }    
    return (
        <>
            <div className="bodyCon projectCon">
                <div className="bodyComponent">
                    <div className="aboutHeading projectHeading" style={Css}>
                        <h1>{props.name}</h1>
                        <div className="container">
                            <div className="projects">
                                
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </>
    )
}

this is a custom component

import React from 'react'
import ProjectTemplate from '../Projects/ProjectTemplate/ProjectTemplate'

export default function Blog(props) {
  return (
    <>
      <ProjectTemplate name='Blog' mode={props.mode} isValue={props.isValue} >
        hhhh
        
      </ProjectTemplate>
    </>
  )
}

this is the another component where i want to add the body of previous component dynamically, then the hhh is not display in browser

output in browser:

<div className="bodyCon projectCon">
                <div className="bodyComponent">
                    <div className="aboutHeading projectHeading" style={Css}>
                        <h1>{props.name}</h1>
                        <div className="container">
                            <div className="projects">
                                hhh
                            </div>
                        </div>
                    </div>
                </div>
            </div>

but hhh is not visible in browser, how can i do for this output

You should read this docs , all you need here is children props

The word hhh will not be displayed because you are calling the custom component in the project template with no props name which you want to display. In your code if you render the component the Blog name will displayed only.

use this in the project template and pass the props to the childrenanme

<div className="projects">
  {props.childrenanme}                       
 </div>

To Answer your Question: Yes we can use component inside nested component in react.

In JSX expressions that contain both an opening tag and a closing tag, the content between those tags is passed as a special prop React Documentation .

And to use these special props you have to use {props.children} .

In your example, you have passed the content b/w opening and closing tag of a custom component but forgot to use it.

In projectTemplate component use the children that you have passed while invoking the component, like this:

<div className="projects">
  {props.childrenanme}                       
 </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM