【问题标题】:components inside components in react with vanilla JS组件内的组件与 vanilla JS 发生反应
【发布时间】:2017-12-26 14:38:35
【问题描述】:

出于命运的考虑,我只能使用 ES3 标准来处理 react.js。所以,jsx,类和其他东西。
使用 React.createClass 和 React.createElement 我设法创建了一个交互式输入组件,它将输入文本反映到输入字段上方的标签。
代码如下:

requirejs.config({
    // module name mapped to CDN url
    paths: {
        // Require.js appends `.js` extension for you
        'react': 'https://unpkg.com/react@15.3.2/dist/react',
        'react-dom': 'https://unpkg.com/react-dom@15.3.2/dist/react-dom'
    }
});

requirejs(['react', 'react-dom'], function(React, ReactDOM) {
      'use strict';
      var Input;
      var cr, root;
      cr = React.createElement;

      Input = React.createClass({
        displayName: 'Input',

        getInitialState: function getInitialState() {return {typed: ''};},

        onChange: function onChange(event) {
          this.setState({typed: event.target.value});
        },

        render: function render() {
          return cr('div', null,
            cr('div', null, '=>' + this.state.typed),
            cr('input', { type: 'text', onChange: this.onChange.bind(this)})
            );
          }
        });

        root = Input;
        var app = document.getElementById('app');
        ReactDOM.render(cr(root, null), app);
});

但现在我正在尝试创建一个包含两个 Input 组件的新组件:

    InputBlock = React.createClass({
    displayName: 'InputBlock',
    render: function render() {
      return cr('div', null, 
        cr('Input', null), 
        cr('Input', null)
        );
      }
    });

我希望看到两个具有动态行为的元素,但只得到两个简单的输入字段:

我知道我错过了一些明显的东西,但我对 react.js 还是很陌生,还看不到全貌。 请帮忙。

最终代码的完整实现

requirejs.config({
    // module name mapped to CDN url
    paths: {
        // Require.js appends `.js` extension for you
        'react': 'https://unpkg.com/react@15.3.2/dist/react',
        'react-dom': 'https://unpkg.com/react-dom@15.3.2/dist/react-dom'
    }
});

requirejs(['react', 'react-dom'], function(React, ReactDOM) {
      'use strict';
      var Input, InputBlock;
      var cr, root;
      cr = React.createElement;

      Input = React.createClass({
        displayName: 'Input',

        getInitialState: function getInitialState() {return {typed: ''};},

        onChange: function onChange(event) {
          this.setState({typed: event.target.value});
        },

        render: function render() {
          return cr('div', null,
            cr('div', null, '=>' + this.state.typed),
            cr('input', { type: 'text', onChange: this.onChange.bind(this)})
            );
          }
        });

        InputBlock = React.createClass({
        displayName: 'InputBlock',
        render: function render() {
          return cr('div', null, 
            cr('Input', null), 
            cr('Input', null)
            );
          }
        });

        root = InputBlock;
        var app = document.getElementById('app');
        ReactDOM.render(React.createElement(root, null), app);
});

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    您不想在 Input 周围加上引号。作为字符串,它使用内置输入,而您需要自定义组件。这段代码应该适用于 InputBlock:

    InputBlock = React.createClass({
      displayName: 'InputBlock',
      render: function render() {
        return cr('div', null, 
          cr(Input, null), 
          cr(Input, null)
        );
      }
    });
    

    【讨论】:

    • 太棒了!这对我很有帮助!购买方式你知道为什么我收到很多关于这段代码的警告,文本相同:警告:bind():你正在将组件方法绑定到组件。 React 以高性能的方式自动为您完成此操作,因此您可以安全地删除此调用。见输入
    • 是的,就是这一行cr('input', { type: 'text', onChange: this.onChange.bind(this)})。你应该可以通过{ type: 'text', onChange: this.onChange}。正如警告消息所说, React.createClass 自动绑定方法。如果您使用的是 ES6 类组件,则必须手动绑定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2021-04-18
    • 2021-03-14
    • 1970-01-01
    • 2019-05-19
    • 2021-03-19
    • 2022-01-15
    相关资源
    最近更新 更多