Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5edca4d

Browse files
child event bubbled - post request
1 parent e9564f4 commit 5edca4d

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

‎Practice/Express-React/backend/routes/files.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ router.get('/', (req, res, next) => {
2828
})
2929

3030
router.post('/', (req, res, next) => {
31+
console.log(req.body);
3132
UserModel
3233
.getCollection()
3334
.then((collection) => {

‎Practice/Express-React/frontend/src/components/FileContent.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
22

3+
import UserForm from './UserForm';
4+
35
class FileContent extends React.Component {
46

57
constructor(props) {
@@ -35,24 +37,25 @@ class FileContent extends React.Component {
3537
});
3638
}
3739

38-
makeUser(e) {
40+
makeUser(data) {
41+
console.log("Sending data")
42+
console.log(data);
3943
fetch('http://localhost:9000/api/files',
4044
{
4145
method: 'POST',
4246
mode: 'cors',
4347
headers: {
4448
'Content-Type': 'application/json'
4549
},
46-
body: JSON.stringify({
47-
name: e.target.value
48-
})
50+
body: JSON.stringify(data)
4951
})
5052
.then((response) => {
5153
console.log(response);
5254
})
5355
.catch((error) => {
5456
console.log(error);
55-
});
57+
}
58+
);
5659
}
5760

5861
// componentDidMount() {
@@ -64,10 +67,11 @@ class FileContent extends React.Component {
6467
<div>
6568
<button onClick={this.makeApiCall}> Call API </button>
6669
<p>{this.state.content}</p>
67-
<form>
70+
<UserForm submitPropagator={this.makeUser} />
71+
{/* <form>
6872
<input type="text" placeholder="Name" />
6973
</form>
70-
<button onClick={this.makeUser} value="Ben">Create User</button>
74+
<button onClick={this.makeUser} value="Ben">Create User</button> */}
7175
</div>
7276
);
7377
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react';
2+
3+
class UserForm extends React.Component {
4+
5+
constructor(props) {
6+
super(props);
7+
this.state = {};
8+
this.prepareSubmit = this.prepareSubmit.bind(this);
9+
}
10+
11+
prepareSubmit(e) {
12+
e.preventDefault();
13+
var data = {};
14+
console.log("HERE");
15+
console.log(e.target.parentNode.children.length);
16+
for (var node of e.target.parentNode.children) {
17+
if (node.name) {
18+
data[node.name] = node.value;
19+
}
20+
}
21+
this.props.submitPropagator(data);
22+
}
23+
24+
render() {
25+
return (
26+
<form>
27+
<input type="text" placeholder="Name" name="name" />
28+
<input type="text" placeholder="Username" name="username" />
29+
<input type="password" placeholder="Password" name="password" />
30+
<button onClick={this.prepareSubmit}>Create User</button>
31+
</form>
32+
);
33+
}
34+
}
35+
36+
export default UserForm;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /