React is a front-end library for building user interfaces, it is not intended to interact with the server directly. However, you can use a backend technology to build a server and have React communicate with that backend through HTTP requests. There are several server-side technologies to choose from like Node.js, Ruby on Rails, Django, ASP.NET, etc.
Here's a simple example of how to create a server-side component in React:
25 chars2 lines
index.tsx391 chars19 lines
Start the server by running node server.js
in the terminal.
In a React component, you can fetch the data from the backend API using the fetch
method or any similar Ajax library like axios
.
index.tsx447 chars23 lines
This example shows how to create a simple backend API with Express that returns an array of objects. Then, in a React component, we use the fetch
method to call that API and display the returned data on the page.
Note: This is a very basic example, and in real production applications, the setup may be more complex.
gistlibby LogSnag