【问题标题】:Can a React component render another component stored in it's state?React 组件可以渲染存储在其状态中的另一个组件吗?
【发布时间】:2019-04-23 11:59:53
【问题描述】:

我试图创建一个容器组件,它可能会随着时间的推移而改变其内部组件。

class Container extends React.Component {
    constructor(props) {
        super(props)
        this.state = { content: new FooComponent() }
    }
    render() {
        return <div>
            { this.state.content }
        </div>
    }
}

我收到如下错误:

对象作为 React 子对象无效(找到:带有键 {props, context, refs, updater, state} 的对象)。如果您打算渲染一组子项,请改用数组。

我试过{ [this.state.component] },但得到同样的错误。

您是否可以将来自this.state 的组件放入从render() 返回的 JSX 中?如果没有,你如何实现一个容器?我正在考虑创建一个类似于 iOS 的 UINavigationController 的容器,我可以在其中推送和弹出堆栈中的组件,而只有顶部的组件呈现到屏幕上。

【问题讨论】:

  • new 将返回一个对象 (this) 而不是 react 组件,使用这个:content: &lt;FooComponent /&gt;,稍后将转换为 content: React.createElement(FooComponent, .....),但是将 react 组件存储在 state 中不是一个好主意,最好将相关信息存储在状态中以呈现该组件。查看MDN Doc,了解有关new 运算符如何工作 的更多详细信息。

标签: reactjs


【解决方案1】:

你可以使用JSX:this.state = { content: &lt;FooComponent/&gt; }

幕后React.createElement()。

class Container extends React.Component {
    constructor(props) {
        super(props)
        this.state = { content: <FooComponent/> }
    }
    render() {
        return <div>{this.state.content}</div>;
    }
}

或参考Creating React Elements。

这里有更易读的表格供参考:

class Container extends React.Component {
  state = {
    content: <FooComponent />
  };

  render() {
    return <>{this.state.content}</>;
  }
}
function Container() {
  const [content, setContent] = useState(<FooComponent />);

  return <>{content}</>;
}

【讨论】:

    【解决方案2】:

    一个 React 组件可以渲染另一个存储在它的 state 中的组件吗?

    是的,你可以!

    有两种方法

    1

    import FooComponent from '..'
    
    ...
    this.state = {
        content: <FooComponent/>
    }
    ...
    render(){
        return (
            <div>{this.state.content}</div>
        )
    }
    

    或

    2

    import FooComponent from '..'
    
    ...
    this.state = {
        content: FooComponent
    }
    ...
    render(){
        const {
            content: BarComponent
        } = this.state
    
        return (
            <div>
                <BarComponent />
            </div>
        )
    }
    

    我收到如下错误:

    对象作为 React 子对象无效(找到:带有键 {props, context, refs, updater, state} 的对象)。如果您打算渲染一组子项,请改用数组。

    • 发生这种情况是因为当您执行new FooComponent() 时,它会返回一个对象并且您无法渲染对象。
    • 当使用 JSX 时,将被转译为 React.createElement(FooComponent) 并且这是可渲染的。
    • 当仅使用FooComponent 时,您存储对它的引用,并且仅在渲染方法中创建一个将变为React.createElement(BarComponent) 的 JSX

    当组件来自 props 时,第二种方法很好,你根据情况想给它一些 props。

    工作Example

    【讨论】:

      【解决方案3】:

      主要问题可能已经回答there。

      注意:功能有限 - 仅存储组件将是一个简单的无状态视图/屏幕堆栈/缓冲区。

      问题可能更广泛。此解决方案不等于保存以前的状态、更改……没有道具/参数、没有历史记录、没有上下文、状态反转……但是共享公共状态(redux 存储)可能是一个优势 - 没有旧状态,自动更新整个存储视图链。

      【讨论】:

        【解决方案4】:
        class Container extends React.Component {
         constructor(props) {
            super(props)
            this.state = { content: this.FooComponent() }
          }
        
         FooComponent(){
          return(<FooComponent/>)
         }
        
        render() {
            return <div>
                { this.state.content }
            </div>
         }
        }
        

        【讨论】:

        • 你也可以通过这种方式渲染jsx或组件
        猜你喜欢
        • 2020-10-21
        • 1970-01-01
        • 2019-10-14
        • 2017-12-10
        • 2018-01-26
        • 2020-08-28
        • 1970-01-01
        • 2021-02-16
        • 2021-12-04
        相关资源
        最近更新 更多