简体   繁体   English

使用 React.createElement 时出错

[英]Error while using React.createElement

I am using reactJS and following file is the client side code which i later bundle using browserify.我正在使用 reactJS 并且以下文件是我稍后使用 browserify 捆绑的客户端代码。
I am able to create elements such as check box input etc. but i am having issue while creating element for Router.我能够创建诸如复选框输入等元素,但在为路由器创建元素时遇到问题。
This is ok -还行吧 -

React.createElement('input', { ref: 'done', type: 'checkbox', defaultChecked: this.state.done, onChange: this.onChange })

I am having issue with below code -我在使用以下代码时遇到问题 -

var React = require('react');
var ReactDOM = require('react-dom');
var TodoItem = require('../lib/components/todo-item');
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var browserHistory = require('react-router').browserHistory;
var renderTarget = document.getElementById('content');
TodoItem.component = React.cloneElement(TodoItem.component, {done: false, name: 'Write Tutorial'});
var TodoItemFactory = React.createFactory(TodoItem.component);

ReactDOM.render(
  React.createElement(
    Router,
    { history: browserHistory },
    React.createElement(Route, {path: "/", component: TodoItemFactory })
  ),
  renderTarget);

In the browser at ReactDOM.render ... the error is reported as -在 ReactDOM.render 的浏览器中......错误报告为 -

warning.js:45 Warning: React.createElement: type should not be null, undefined, boolean, or number. warning.js:45 警告:React.createElement:类型不应为空、未定义、布尔值或数字。 It should be a string (for DOM elements) or a ReactClass (for composite components).它应该是一个字符串(对于 DOM 元素)或一个 ReactClass(对于复合组件)。 Check the render method of bound .检查bound的渲染方法。

For reference - lib/components/todo-item.js -供参考 - lib/components/todo-item.js -

'use strict';

var React = require('react');

var TodoComponent = React.createClass({
  displayName: 'TodoItem',

  /**
   * Lifecycle functions
   **/
  getInitialState: function getInitialState() {
    return { done: this.props.done };
  },

  componentDidMount: function componentDidMount() {},

  render: function render() {
    return React.createElement(
      'label',
      null,
      React.createElement('input', { ref: 'done', type: 'checkbox', defaultChecked: this.state.done, onChange: this.onChange }),
      this.props.name
    );
  },

  /**
   * Event handlers
   **/
  onChange: function onChange(event) {
    this.setDone(event.target.checked);
  },

  /**
   * Utilities
   **/
  setDone: function setDone(done) {
    this.setState({ done: !!done });
  }
});

module.exports.component = TodoComponent;

I believe what is tripping you up is that you are exporting the component as module.exports.component = TodoComponent .我相信让您module.exports.component = TodoComponent是您将组件导出为module.exports.component = TodoComponent

Its just cleaner to do it like this:这样做更干净:

module.exports = TodoComponent;

And that way you can avoid the extra dot notation and require it like this.这样你就可以避免额外的点符号并像这样要求它。

var TodoItem = require('../lib/components/todo-item');

Since you didn't do that you are now working with this TodoItem.component thing - and I think reassigning it with the cloned element is causing the error.由于您没有这样做,您现在正在使用此TodoItem.component事物 - 我认为用克隆元素重新分配它会导致错误。

Instead, just assign it to a new variable var completedTodoItem = React.cloneElement...相反,只需将它分配给一个新变量var completedTodoItem = React.cloneElement...

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

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