5

I want to learn reactJS is it dependent on a certain backend?

Also, is it possible to use reactJS as frontend then PHP and MySQL for the backend?

2
  • 1
    The whole beauty of the web and http is that the front-end and backend are completely disconnected. As long as they both speak http then you can use any combination you like Commented Sep 17, 2021 at 7:27
  • Nice! Thank you! Commented Sep 17, 2021 at 13:41

3 Answers 3

8

Absolutely! The best approach is to write an API in PHP to pass the MySQL data to javascript via REST endpoints. Like this:

https://code.tutsplus.com/tutorials/how-to-build-a-simple-rest-api-in-php--cms-37000

Then use HTML and Javascript to build a react frontend that would access this data. You'd probably want to use a built tool like webpack or even a framework like next.js to compile your javascript and do nice things like code splitting and tree-shaking.

In the above case, they would be two separate code bases. One for the API and one for the frontend. But react is actually very adaptable and you can run any react component on any page very easily using ReactDOM here is an intro:

https://reactjs.org/docs/add-react-to-a-website.html

with this you can put a react component on any existing php website and either build out specific endpoints to populate data or even just fill a <script> tag with JSON and read it with useEffect or componentDidMount

Sign up to request clarification or add additional context in comments.

1 Comment

Impressive! Thank you for the answer and links! Will check that out!
5

I want to learn ReactJS, is it dependent on a certain backend?

No, it's not. It depends on your project needs and preference.

Is it possible to use ReactJS as frontend, then PHP and MySQL for the backend?

Yes, of course. You can build a backend with PHP using a framework like CodeIgniter, or Laravel, or vanilla PHP, and build your API and serve your React App with the data.

Or you can use Node.js for the backend, or Java spring boot, or .NET, or any backend language. Just build a good REST API and use the language that fits your project. There is a lot more to say.

2 Comments

Oh my god! Thank you for this answer! This gives me a lot of ideas about what will I use for this. Have a good one!
ReactJs was created by facebook and facebook used php on backend before (and till now because some facebook page and url still ending in .php as I checked or saw), so yes
1

Of course, it's possible to use PHP with ReactJS, and I'm not just referring to having two separate layers for implementation (i.e., the API (Backend) layer and ReactJS as a separate frontend layer).

What I mean is that PHP and ReactJS can be combined and shipped in a single application package, much like one would work with vanilla JS or other common front-end tools like jQuery, Bootstrap, etc. I even wrote an article on how to achieve this: PHP and ReactJS: Combined.

I've also gone as far as writing a minimal PHP framework that uses ReactJS as its view handler - called Mimosa.

Note: Just to provide some insight, any npm module that can work via a CDN URL is likely to work with PHP out of the box. To find out which modules you can use over CDN, you can refer to Skypack.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.