简体   繁体   English

将组件传递给React setState值

[英]Passing a component into React setState value

I've got a Meteor app using React. 我有一个使用React的Meteor应用程序。 I've added Session variables and want to pass the new Session value (which will be another React component) into another react component. 我已经添加了Session变量,并希望将新的Session值(将是另一个React组件)传递到另一个React组件中。

The user will click the p-tag in the SideNav and reset the Session to a React component. 用户将单击SideNav中的p标签并将会话重置为React组件。

SideNav component : SideNav组件

import React from 'react';
import { Session } from 'meteor/session';
import SonataContent from './sonata-content';

export default () => {
  injectSonataText = () => {
    const sonataContent = <SonataContent/>;
    Session.set('MainContent', sonataContent); /* Set Session value to component */
  };

  return (
    <div className="side-nav">
      <h2>Explore</h2>
      <p onClick={this.injectSonataText.bind(this)}><i className="material-icons">child_care</i><span>&nbsp;Sonatas</span></p>
    </div>
  )
}

In the MainWindow, Tracker.autorun re-runs and sets the state to the component and renders the new state value. 在MainWindow中,Tracker.autorun重新运行并将状态设置为组件并呈现新的状态值。

Main Window component: 主窗口组件:

import React from 'react';
import { Session } from 'meteor/session';
import { Tracker } from 'meteor/tracker';


export default class MainWindow extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      text: ""
    }
  }

  componentDidMount() {
    this.mainWindowTracker = Tracker.autorun(() => {
      const text = Session.get('MainContent');
      this.setState({text: text});
  });
}

  componentWillUnmount() {
    this.mainWindowTracker.stop();
  }

  render() {
    return (
      <p>{this.state.text}</p>
    )
  }
}

I'm getting an error "Invariant Violation: Objects are not valid as a React child". 我收到一个错误“不变违反:对象作为React子元素无效”。 Is this caused by the component being used in setState? 这是由setState中使用的组件引起的吗? Is there a way to do this? 有没有办法做到这一点?

Session set function accepts as a value EJSON-able Object which I think may not work with React Object. 会话集合函数接受EJSON可用对象作为值,我认为该对象可能不适用于React Object。

However I would try (only a guess though): 但是我会尝试(虽然只是一个猜测):

injectSonataText = () => {
  Session.set('MainContent', SonataContent); /* Set Session value to component */
};

... ...

export default class MainWindow extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      Component: null,
    }
  }

  componentDidMount() {
    this.mainWindowTracker = Tracker.autorun(() => {
      const MainContent = Session.get('MainContent');
      this.setState({Component: MainContent});
  });
}

  componentWillUnmount() {
    this.mainWindowTracker.stop();
  }

  render() {
    const { Component } = this.state;
    return (
      <p>
        {
          Component && <Component />
        }
      </p>
    )
  }
}

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

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