Skip to main content

11.11 Using Routing Related Props

NewPost.js

NewPost.js
import axios from "axios";
import React, { Component } from "react";
import "./NewPost.css";

class NewPost extends Component {
state = {
title: "",
content: "",
author: "Pash",
};
componentDidMount() {
console.log(this.props);
}
postDataHandler = () => {
const post = {
title: this.state.title,
body: this.state.content,
author: this.state.author,
};
axios.post("/posts", post).then((response) => {
console.log(response);
});
};

render() {
return (
<div className="NewPost">
<h1>Add a Post</h1>
<label>Title</label>
<input
type="text"
value={this.state.title}
onChange={(event) => this.setState({ title: event.target.value })}
/>
<label>Content</label>
<textarea
rows="4"
value={this.state.content}
onChange={(event) => this.setState({ content: event.target.value })}
/>
<label>Author</label>
<select
value={this.state.author}
onChange={(event) => this.setState({ author: event.target.value })}
>
<option value="Pash">Pash</option>
<option value="Mash">Mash</option>
<option value="Chel">Chel</option>
</select>
<button onClick={this.postDataHandler}>Add Post</button>
</div>
);
}
}

export default NewPost;

Posts.js

Posts.js
import React, { Component } from "react";
import axios from "../../../axios";
import Post from "../../../components/Post/Post";
import "./Posts.css";

class Posts extends Component {
state = {
posts: [],
};

componentDidMount() {
console.log(this.props);
axios
.get("/posts")
.then((response) => {
const posts = response.data.slice(0, 4);
const updatedPosts = posts.map((post) => {
return {
...post,
author: "Pash",
};
});
this.setState({ posts: updatedPosts });
// console.log(response);
})
.catch((error) => {
console.log(error);
// this.setState({ error: true });
});
}

postSelectedHandler = (id) => {
this.setState({ selectedPostId: id });
};
render() {
let posts = <p style={{ textAlign: "center" }}>Something went wrong!</p>;
if (!this.state.error) {
posts = this.state.posts.map((post) => {
return (
<Post
key={post.id}
title={post.title}
author={post.author}
clicked={() => this.postSelectedHandler(post.id)}
/>
);
});
}
return <section className="Posts">{posts}</section>;
}
}
export default Posts;