2つの反応コンポーネントがあります。私はtextfield
を持っている子コンポーネントで:React:親コンポーネントの状態データを取得
var React = require('react');
var UserInput = React.createClass({
getInitialState: function() {
return {ItemTotype: ''};
},
handleUserInput: function(e){
// console.log(e.target.value);
this.setState({ItemTotype: e.target.value});
},
あなたが見ることができるように、私は、アプリケーションの状態にテキストボックス内のユーザーのタイプに何かするたびに更新するトリングています。
そして、親コンポーネントの状態にデータを挿入したいと思います。これを行うには
と私は親コンポーネント(ルート)で `を使用:
console.log(this.state.data);
が、それは私だけ[]
を与えます。私もconsole.log(this.state.data.ItemTotype);
を試しましたが、今回はundefine
でした。
どうすればよいですか?
paerntコンポーネントは次のとおりです。
define(["./Statistic","./UserInput","./RandomWords","react", "react-dom"],
(Statistic,UserInput,RandomWords,React, ReactDOM) => {
var Root = React.createClass({
loadItemTypedFromServer: function() {
console.log("Hey");
console.log(this.state.ItemTotype);
},
getInitialState: function() {
return {data: []};
},
componentDidMount: function() {
this.loadItemTypedFromServer();
setInterval(this.loadItemTypedFromServer, 1000);
},
render: function() {
return (
<div >
<h1>Welcome to our game</h1>
<RandomWords/>
<UserInput />
<Statistic/>
</div>
);
}
});
ReactDOM.render(
React.createElement(Root, null),
document.getElementById('content')
);
});
UserInputは子コンポーネント –
@Alexanderどのように修正できますか? –