Skip to content

Commit 840c6ee

Browse files
committed
Refactored App props to state
1 parent 5e09fb7 commit 840c6ee

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

wild-things/src/App.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { BrowserRouter as Router, Route } from "react-router-dom";
1616
// Import EmailJs for initiation
1717
import emailjs from "@emailjs/browser";
1818
import { EMAILJS_USER } from "./config/keys";
19+
import { WP_REST_GET_POSTS_URL } from "./config/keys";
20+
import axios from "axios";
1921

2022
// Import main site components
2123
import NavBar from "./components/NavBar";
@@ -36,8 +38,21 @@ import Footer from "./components/Footer";
3638
* @returns primary router component of the App
3739
*/
3840
class App extends Component {
41+
state = { posts: [], selectedPost: "" };
42+
3943
componentDidMount() {
40-
console.log("app props", this.props.posts);
44+
let blogPosts = [];
45+
axios.get(WP_REST_GET_POSTS_URL).then((response) => {
46+
blogPosts = response.data;
47+
for (let blog of blogPosts) {
48+
blog.excerpt.rendered = blog.excerpt.rendered
49+
.replace(/(^"|"$)/g, "")
50+
.replace("[", "")
51+
.replace("]", "");
52+
}
53+
this.setState({ posts: blogPosts });
54+
console.log("state", this.state);
55+
});
4156
}
4257

4358
render() {
@@ -48,15 +63,15 @@ class App extends Component {
4863
<ScrollToTop />
4964
<NavBar />
5065
<Route exact path="/">
51-
<LandingPage posts={this.props.posts} />
66+
<LandingPage posts={this.state.posts} />
5267
</Route>
5368
<Route exact path="/about">
5469
<AboutPage />
5570
</Route>
5671
<Route exact path="/posts">
57-
<PostsPage posts={this.props.posts} />
72+
<PostsPage posts={this.state.posts} />
5873
</Route>
59-
<Route exact path="/post" posts={this.props.posts}>
74+
<Route exact path="/post" posts={this.state.posts}>
6075
<PostPage />
6176
</Route>
6277
<Route exact path="/contact">

wild-things/src/index.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import React from "react";
1111
import ReactDOM from "react-dom";
12-
import axios from "axios";
1312

1413
// Import styling, which is compiled from Sass - customized Bootsrap v5.3
1514
import "glightbox/dist/css/glightbox.css";
@@ -21,27 +20,9 @@ import "bootstrap/dist/js/bootstrap.js";
2120

2221
import App from "./App";
2322

24-
import { WP_REST_GET_POSTS_URL } from "./config/keys";
25-
26-
const startApp = async () => {
27-
let blogPosts = [];
28-
await axios.get(WP_REST_GET_POSTS_URL).then((response) => {
29-
console.log("axios called");
30-
blogPosts = response.data;
31-
for (let blog of blogPosts) {
32-
blog.excerpt.rendered = blog.excerpt.rendered
33-
.replace(/(^"|"$)/g, "")
34-
.replace("[", "")
35-
.replace("]", "");
36-
}
37-
38-
ReactDOM.render(
39-
<React.StrictMode>
40-
<App posts={blogPosts} />
41-
</React.StrictMode>,
42-
document.getElementById("root")
43-
);
44-
});
45-
};
46-
47-
startApp();
23+
ReactDOM.render(
24+
<React.StrictMode>
25+
<App />
26+
</React.StrictMode>,
27+
document.getElementById("root")
28+
);

0 commit comments

Comments
 (0)