2017-12-26 15 views
0

運命の意志によって、私はreact.jsでES3 standradだけを使用して作業する必要があります。だから、jsx、クラスと他のもの。
React.createClassとReact.createElementの使用入力フィールドの上のラベルに入力テキストを反映する対話型入力コンポーネントを作成しました。 enter image description here
このためのコードは以下の通りです:コンポーネント内のコンポーネントがバニラJSと反応する

requirejs.config({ 
    // module name mapped to CDN url 
    paths: { 
     // Require.js appends `.js` extension for you 
     'react': 'https://unpkg.com/[email protected]/dist/react', 
     'react-dom': 'https://unpkg.com/[email protected]/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); 
}); 

が、今、私は内部の二つの入力コンポーネントと新しいコンポーネントを作成しようとしています:

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

そして、私は2つの要素を参照してくださいすることが期待しました動的な振る舞いですが、2つの単純な入力フィールドしか得られません: enter image description here

私は明らかに何かが間違っていることを理解していますが、 react.jsで新しいですし、まだ全体の画像を見ることはできません。 お願いします。

最終的なコードの完全な実装

requirejs.config({ 
    // module name mapped to CDN url 
    paths: { 
     // Require.js appends `.js` extension for you 
     'react': 'https://unpkg.com/[email protected]/dist/react', 
     'react-dom': 'https://unpkg.com/[email protected]/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); 
}); 

答えて

0

あなたは、入力の前後に引用符を持っている必要はありません。文字列として、組み込みの入力を使用していますが、カスタムコンポーネントが必要です。このコードはInputBlockで動作するはずです:

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

それは私をたくらんでくれる!警告:bind():コンポーネントメソッドをコンポーネントにバインドしています。このメソッドを使用すると、同じコードで多くの警告を受け取ることができます。 Reactはこれを自動的に高性能で行いますので、この呼び出しを安全に削除することができます。入力 –

+0

を参照してください。これは 'cr( 'input'、{type: 'text'、onChange:this.onChange.bind(this)})'の行です。 '{type: 'text'、onChange:this.onChange}'だけを渡すことができます。警告メッセージが示すように、React.createClassはメソッドを自動バインドします。 ES6クラスコンポーネントを使用していた場合、手動でバインドする必要があります。 – TLadd

関連する問題