35
loading...
This website collects cookies to deliver better user experience
src
└──app
└── components
├── CreatePost.js
├── ListPost
├── ShowPost.js
├── InPost.js
├── Reply.js
└──store
├── store.js
├── PostSlice.js
├── app(router).js
🤘 Redux
"@reduxjs/toolkit": "^1.6.2"
"react-redux": "^7.2.6"
🔥 Firebase
"firebase": "^9.5.0"
"react-firebase-hooks": "^4.0.1"
⬅⬇➡ Router
"react-router": "^5.2.1"
"react-router-dom": "^5.3.0"
import React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
<Router>
<Switch>
<Route exact path="/blabla" component={blabla} />
</Switch>
</div>
</Router>
const inputTitle = useRef("");
const inputMessage = useRef("");
const db = firebase.firestore();
const sendPost = useCallback(async event => {
event.preventDefault();
if (inputTitle.current.value !== "") {
db.collection("post").add({
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
title: inputTitle.current.value,
message: inputMessage.current.value,
name: user.displayName,
photoURL: user.photoURL,
});
}
})
return (
/* the onSubmit will execute the "sendPost" function. */
<form onSubmit="sendPost">
<input
ref={inputTitle}
placeholder="Title"
>
<input
ref={inputMessage}
placeholder="Message"
>
<button type="submit">
</form>
)
console.firebase.google.com
, it would look like this:import { useCollection } from "react-firebase-hooks/firestore"
import ShowPost from './ShowPost.js'
const db = firebase.firestore();
const [postlist] = useCollection(
db
.collection("post")
);
return(
/* Don't forget to keep the key and the id, it will be important for the future. */
{postlist?.docs.map((doc) => (
<MessageShow
key={doc.id}
id={doc.id}
title={doc.data().title}
name={doc.data().name}
photoURL={doc.data().photoURL}
/>
))}
)
{blabla}
so that the text of the field is displayed.function ShowPost({id, title, name, photoURL}) {
return (
<img src={photoURL}>
<p> {title} </p>
<p> {name} </p>
)
/* We import the configureStore from redux */
import { configureStore } from "@reduxjs/toolkit";
import postSlice from "./PostSlice";
export const store = configureStore({
reducer: {
post: postSlice,
},
});
'PostSlice.js'
import { createSlice } from "@reduxjs/toolkit";
/* We define the initial state */
const initialState = {
postId: null,
};
export const postSlice = createSlice({
name: "post",
initialState,
reducers: {
setPost: (state, action) => {
state.postId = action.payload.postId;
state.postName = action.payload.postName;
},
},
});
export const { setPostInfo } = postSlice.actions;
export const selectPostId = (state) => state.post.postId;
export const selectPostName = (state) => state.post.postName;
export default postSlice.reducer;
' index.js '
/* We import our redux tools */
import { Provider } from "react-redux"
import { store } from "./store/Store"
/* We add a provider */
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
MessageShow.js
filefunction ShowPost({id, title, name, photoURL}) {
const setPost = () => {
dispatch(
setPostInfo({
postId: id,
})
);
history.push(`/post/{$id}`);
};
return (
/* We now add a div, and when we click on it, it executes the 'setPost' function
We also define the cursor as a pointer
*/
<div onClick={setPost} style={{cursor: 'pointer'}}>
<img src={photoURL}>
<p> {title} </p>
<p> {name} </p>
</div>
)
app (router.js)
/* You just add these two little lines.
Thanks to what we have not added in index.js, we can freely put the id in the router. When it is on the url /post /id/, it will display the component 'InPost.js'
*/
import InPost from './InPost.js'
return (
<Route exact path="/post/message/:id" component={InPost} />
)
InPost.js
/* We import all the features */
import { useCollection } from "react-firebase-hooks/firestore"
import { selectPostId } from './store'
import { useSelector } from "react-redux";
fonction InPost(){
const db = firebase.firestore();
const postId = useSelector(postId);
/* The code is almost identical to MessageShow.js, it's just that we add a .doc(postId)*/
const [InPostShow] = useCollection(
db
.collection("front-message")
.doc(postId)
);
return (
{InPostShow?.docs.map((doc) => {
const { title, message, photoURL, name} = doc.data()
return (
<p>{tile}</p>
<p>{message}</p>
);
})}
)
}
CreatePost.js
and ShowMessage.js
Bitcoin | Ethereum |
---|---|
3FahtNWC4tmZh1B72vz44TvBN2jHaQSnh4 |
0x7cad12dfd11bce3f29b96260b4739caa32c89a86 |