F
F
Faiq Alizade2018-06-18 07:54:19
HTML
Faiq Alizade, 2018-06-18 07:54:19

How to render react component from html?

Hello, in Vue.js, you can render (display) a component by writing the name of the component as a tag in html.
This can be useful with PHP while looping as much as necessary. I want to do the same with React or similar.
Reason: PAGE WITH ARTICLES WITH DB.EACH ARTICLE COMPONENT IN THIS COMPONENTS MUST BE PUBLISHED DATES I WANT TO PASS A COMPONENT WITH PHP TO REACT
EXAMPLE:
PHP

<?php
foreach($posts as $post):
?>
<my-component  date='<?=$post->date?>' /> (Или похожее)
<?php endforeach; ?>

JS
class my-component extends React.Component{
render(){
return (
    <div>
         this.props.date (Например)
    </div>
)
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-06-18
@faiq

Wrong approach to development. Options for how to do this correctly:
1. The data is collected by an Ajax API request
2. If you want to give data with the page, you can write to any tag in the data-state attribute or in the script tag, in the window.__data
key 3. You can write a node server with SSR and render on the server side.

A
Anubis, 2018-06-19
@Anubis

What kind of code should be in the script for NodeJS to perform SPA server rendering on React?
Please see my answer, I described an example of server rendering of a React application on Node (there is Redux, for Mobx the scheme is almost the same, with minor differences). For php, I'm sure there are solutions too, if it's important for you to use php on the server (but I would advise you to bury this dinosaur and look towards Node).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question