简体   繁体   English

在另一个 React 中渲染组件

[英]Render a component inside another React

Ok i'm so lost, i tryied finding the answer in old threads but i couldn't find anything that would help me.好的,我迷路了,我尝试在旧线程中找到答案,但找不到任何可以帮助我的东西。 My code looks like this我的代码看起来像这样

class Background extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            bgColor: [
                'red',
                'blue',
                'yellow',
              ],
              selectedColor: ''
        }
    }
    componentDidMount(){
        this.getRandomColor();
    }
    getRandomColor(){
        var item = this.state.bgColor[Math.floor(Math.random()*this.state.bgColor.length)];
        this.setState({
          selectedColor: item,
        })
      }
      render(){
          return(
            <div style={{backgroundColor: this.state.selectedColor}}>
                <h1>Quote Generator</h1>
            </div>
          )
      }
}

class Count extends React.Component{
    constructor(props){
        super(props);
        this.state={
            count: 0
        }
    }
    addNumber(){
        this.setState({
            count: this.state.count +1
        })
    }
    render(){
        return(
            <div>
                <h1>{this.state.count}</h1>
                <button onClick={this.addNumber.bind(this)}>ADD A NUMBER</button>
            </div>
        );
    }
}

class QuoteGenerator extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        return(
            <Background>
                               <Count/>
            </Background>
            

        );
    }
}

ReactDOM.render(<QuoteGenerator/>, document.getElementById('root'));

the problem is that it only renders the Background component, while the Count component remains invisible, no errors or anything, and i can't figure out why, any help is gladly appreciated.问题是它只呈现背景组件,而计数组件仍然不可见,没有错误或任何东西,我不知道为什么,很高兴有任何帮助。

You want to use props.children in your Background component您想在Background组件中使用props.children

render(){
   return(
      <div style={{backgroundColor: this.state.selectedColor}}>
         <h1>Quote Generator</h1>
         {this.props.children}
      </div>
   )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用getter在React组件内渲染另一个组件是一种好习惯吗? - Is it a good practice to use getter to render another component inside React component? React –如何在另一个已安装的组件内部渲染其他组件? - React – how to render additional component inside another mounted one? 函数内部的渲染组件 - Render component inside a function react 在React中渲染另一个组件 - Render a component within another in React React JS 从另一个组件渲染组件 - React JS render component from another component 渲染另一个组件的渲染器 function 中声明的组件不包括 REACT 17 中 componentDidMount 方法的更改 - Rendering a component declared inside the render function of another component does not include changes in the componentDidMount method in REACT 17 当我的组件被传递给另一个组件进行渲染时,React 抱怨没有在主体 function 内部使用钩子 - React complains that hook is not used inside of a body function when my component is passed into another component to render 在React组件内渲染React元素 - Render a React element inside a React component 如何在React组件中渲染Backbone视图? - How to render a Backbone view inside a React component? 使用反应组件的变量内部渲染功能 - use of variable inside render function of react component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM