javascript(react)について
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 1,222
react入門サイト(http://www.fumiononaka.com/Business/html5/FN1608006.html)をお借りして、勉強しています。
こちらのプログラムで処理したいことは以下となります。
テキストボックスに文字を入力し、送信ボタンを押すと、jsonファイルに「id,author,text」が保存され、その文字が画面上に表示されるというものです。初期登録されているjsonデータは読み取れているのですが、保存がうまくいかない状態です。
console.logなどを追って確認しているのですが、原因がわからず困り果てています。
ご指導ご鞭撻お願いできないでしょうか。
● ファイル保存内容
demo5
|- node_modules ・・・
|- src - script.js
|- comment.json
|- index.html
|- package.json
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample</title>
<script src="https://npmcdn.com/react@15.3.0/dist/react.min.js"></script>
<script src="https://npmcdn.com/react-dom@15.3.0/dist/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/remarkable/1.6.2/remarkable.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="src/script.js" type="text/babel"></script>
</head>
<body>
<div id="content"></div>
</body>
</html>
// script.js
var CommentBox = React.createClass({
getInitialState: function() {
console.log("CommentBox :getInitialState通過");
return {data: []};
},
componentDidMount: function() {
console.log("CommentBox :componentDidMount通過");
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, error) {
console.error(this.props.url, status, error.toString());
}.bind(this)
});
},
handleCommentSubmit: function(comment) {
console.log("CommentBox :handleCommentSubmit通過");
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: comment,
success: function(data) {
console.log(comment);
console.log(data)
this.setState({data: data});
}.bind(this),
error: function(xhr, status, error) {
console.error(this.props.url, status, error.toString());
}.bind(this)
});
},
render: function() {
console.log("CommentBox :render通過");
return (
<div className="commentBox">
<h1>コメント</h1>
<CommentList data={this.state.data} />
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
</div>
);
}
});
var CommentList = React.createClass({
render: function() {
console.log("CommentList:render通過");
var commentNodes = this.props.data.map(function(comment) {
console.log("CommentList:commentNodes通過");
return (
<Comment author={comment.author} key={comment.id}>
{comment.text}
</Comment>
);
});
return (
<div className="commentList">
{commentNodes}
</div>
);
}
});
var Comment = React.createClass({
render: function() {
console.log("Comment :render通過");
return (
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div>
);
}
});
var CommentForm = React.createClass({
getInitialState: function() {
console.log("CommentForm:getInitialState通過");
return {author: '', text: ''};
},
handleChange: function(eventObject) {
console.log("CommentForm:handleChange通過");
var state = {};
state[eventObject.target.id] = eventObject.target.value;
this.setState(state);
},
handleSubmit: function(eventObject) {
console.log("CommentForm:handleSubmit通過");
eventObject.preventDefault();
var author = this.state.author.trim();
var text = this.state.text.trim();
if (!author || !text) {
alert("入力してください");
return;
}
this.props.onCommentSubmit({author: author, text: text});
this.setState({author: '', text: ''});
},
render: function() {
console.log("CommentForm:render通過");
return (
<form className="commentForm" onSubmit={this.handleSubmit}>
<input
type="text"
id="author"
placeholder="名前"
value={this.state.author}
onChange={this.handleChange}
/>
<input
type="text"
id="text"
placeholder="コメントを入力"
value={this.state.text}
onChange={this.handleChange}
/>
<input type="submit" value="送信" />
</form>
);
}
});
console.log("ReactDOM.render通過");
ReactDOM.render(
<CommentBox url="…localhost/reacttest/demo5/comment.json" />,
document.getElementById('content')
);
[
{
"id": 1633374400000,
"author": "テスト太郎",
"text": "表示データ"
}
]
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
どこまでうまくいっているのか確認しましょう。
- 可能性としては、AjaxでPOSTでデータを送るまでにデータがちゃんとできてない可能性です。データを送っている
$.ajax(...
の手前でcomment
の中身が想定通りが確認してみてください。 - 次はサーバー側が動いていない場合です。その前の前のページ「React入門 03」の所でserver.jsというのを用意したと思います。それがきちんと動いているか確認してみてください。同じように、POSTを受け取った処理の所で、データが想定通りなのかを確認するといいでしょう。
もし、1.がうまくいっているのであれば、server.jsがおかしい可能性が高いです。経験上、react-tutorialにあるserver.jsが壊れて動かなかったときもあります(Node.jsとのバージョン違いとかで駄目だったらしい)。server.jsのなかみやNode.jsのバージョンもわかれば、何かわかるかもしれません。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.13%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/02/01 17:50
console.log(comment);で細かくデータを確認していきましたところデータはうまく読み取れていたようです。server.jsに重点を置いて調べなおしてみようと思います。一つ一つ確認していくことが大切ですね。