score:11
I think your best option is to indeed make some kind of network request from the client. If you aim to keep the app simple and do not want a State Management library (e.g. Redux), you could do something like
class IndexPage extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentDidMount() {
fetch('/') // or whatever URL you want
.then((response) => response.json())
.then((posts) => this.setState({
posts: posts,
});
}
render() {
return (
<div>
<Posts posts={this.state.posts} />
</div>
);
}
}
In your response
there should be a JSON representation of the posts collection.
Also note the render
method and accessing the posts
.
For more on Fetch API see MDN (please also note that you will need a polyfill for older browsers for it).
EDIT: For socket.io I'd store the instance of it somewhere and pass it as a prop to the component. Then you can do something like
class IndexPage extends React.Component {
...
componentDidMount() {
this.props.socket.on('postReceived', this.handleNewPost);
}
handleNewPost = (post) => {
this.setState({
posts: [
...this.state.posts,
post,
],
});
}
...
}
The server-side part is similar, see for example Socket.io Chat example.
Source: stackoverflow.com
Related Query
- How can I pass data from express server to react views?
- How can i pass data from promise to props in react
- Pass Data from React to node express server
- How do you pass data to react components from express or koa without renderToString?
- How can I send my Instagram access token from my Express server to React client?
- React - How can I pass API data from one component to another in a different js file?
- How can I pass a unique identifier from my express backend server to my frontend reactjs to be displayed in my web application?
- How do I display data from my express server in React frontend?
- How to get data from React aplication on Express server to update meta-tags with data fetched from API
- How to get data from backend express server to frontend react app and vice versa
- How to pass data from React front end Hooks to Express backend
- How do I pass data from react front end to node express backend?
- How can I pass a variable from 'outside' to a react app?
- How can I pass data using <Redirect> in react router v4?
- how can I show customized error messaged from server side validation in React Admin package?
- How can I get data from a local file into my React app?
- How to post data from react to express
- How to send form data from React to express
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How to pass data from React Form -> Flask Backend -> React Component (does it have something to do with CORS)?
- How can I cache data from API to Cache Storage in React PWA?
- How can I use react useContext , to show data from context, in the same component
- How to activate a react route and pass data from the service worker?
- How to pass data from one component to another in React or React-Redux?
- How to pass server data into React component
- React - How to pass returned data from an exported function to a component?
- How to pass data from a page to another page using react router
- REACT JS How can I display my fetched API data from onClick?
- How to inject data from the server in index.html in an ASP.NET Core 3 React app
- How to Fetch and Display an Image from an express backend server to a React js frontend?
More Query from same tag
- React and CSS gauge progress. SVG circular path length
- How to setState of a sibling component in react with context?
- Is it possible to direct to an external website using variables in React?
- mapDispatchToProps passess undefined props
- Can't load content "Nothing was returned from render"
- Error: Actions must be plain objects. Use custom middleware for async actions. But I don`t have async functions
- Using a button to open a dialog box to get some user input
- Why use useMemo and not useCallback here?
- How to put searchbar into header - react-native-elements
- How do I create a tab spacing inside textarea ReactJS + Bootstrap?
- bootstrap 4 center heading and right align button all in one row
- Typing out react-transition-group handlers in TypeScript
- Pass argument to functional component
- Using external objects as state properties in React
- What is the proper interface for element with "GetWrappedInstance"
- Displaying a string representing HMTL as actual HTML, not string content in F#'s Fable application
- Expected onClick listener to be a function, instead got type object - react redux
- Search Query to Filter Results in React
- How to hide dropdown when a button inside it is clicked
- How can I change the color of the text separately with react hooks?
- Bundle React build results into single html file
- React - two buttons - a click on one of them opens both
- Reactjs Static and Dynamic Routes
- How do I use the State component of a react component?
- Next.js failed external image with extension in url
- Bootstrap toggler button not working in reactjs
- Handle State change in React
- useEffect dependent on another useEffect being executed more than once even with dependency array. why?
- How can i write React.js code in Visual Studio code?
- reactjs handle click outside an element error