Skip to content

Commit 83714da

Browse files
Create README.md
1 parent 7b81add commit 83714da

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# PHP-React-Component
2+
3+
This project aims to mimic react component in php
4+
5+
it's one of the elegant solution to make component based web app via php
6+
7+
# Installation:
8+
`composer require phpreact/component`
9+
10+
# Usage:
11+
12+
To create a component you neet to just extend React\Component class
13+
14+
```php
15+
namespace React\Tag;
16+
use React\Component;
17+
18+
class CustomComponent extends Component{
19+
function render(){
20+
return new div(['style'=> 'border:1px solid #eee;border-radius:4px;max-width:500px;padding:5px;margin:10px'],[
21+
new p(['style'=> 'color:red;background:blue'], 'Hello World'),
22+
new div('Many div')
23+
]);
24+
}
25+
}
26+
```
27+
28+
HTML tags are create via the React Component class
29+
all HTML tags are under namespace React\Tag
30+
31+
To register custom html tag
32+
you just call a static function registerTag
33+
```php
34+
React\Tag::register('tag1');
35+
React\Tag::register(['newtag1', 'newtag2']); //multiple html tags
36+
```
37+
38+
To render your app
39+
```php
40+
echo new CustomComponent;
41+
```
42+
43+
44+
we can now have the ability to mimic reactjs state management.
45+
46+
there some slight difference in apply setState as we need to connect js event setState to php component
47+
48+
Note: make sure the component is wrapped by htmltag (ie: p, div, ...)
49+
50+
```php
51+
class CustomComponent extends Component{
52+
var $state = ['test' => 1];
53+
54+
function componentDidUpdate($prevState, $currState){} //run only when there's state update
55+
56+
function render(){
57+
$test = $this->state->test;
58+
59+
return new div(['style'=> 'border:1px solid #eee;border-radius:4px;max-width:500px;padding:5px;margin:10px'],[
60+
new p(['style'=> 'color:red;background:blue'], 'Hello World',),
61+
new div('Many div'),
62+
new button(['onclick'=> "this.setState({test: ".($test+1)."})"], "set my state ($test)")
63+
]);
64+
}
65+
}
66+
```
67+
68+
69+
70+
71+
# Sample full example
72+
73+
```php
74+
namespace React\Tag;
75+
use React\Component;
76+
77+
78+
include_once 'vendor/autoload.php';
79+
80+
//functional Component
81+
function Cars($children = [], $text){
82+
return new div(['style'=>'display:flex'], array_map(function($v) use($text){
83+
return new Car(['text'=> $v . $text]);
84+
}, $children));
85+
}
86+
87+
function Car($text = ''){
88+
$state = \React\useState(['test'=> 1]);
89+
return new div(['onclick'=> 'this.setState(function(state){ return {test: state.test + 1}})','style'=> [
90+
'color'=> '#fff',
91+
'border-style'=>'solid',
92+
'border-radius'=> 2,
93+
'padding'=>5,
94+
'border-color'=> 'red',
95+
'border-width'=> 2,
96+
'margin' => 5,
97+
'background'=> 'brown',
98+
'width'=> '100%',
99+
'text-align'=> 'center'
100+
]], $text . ' '. $state->test);
101+
}
102+
103+
class App extends Component{
104+
function render(){
105+
return [
106+
new div(['style'=>'color:red'],'test world'),
107+
new div(['style'=>'color:red'], 'cool'),
108+
new Cars(['text'=> ' hello world'],['Volsvagen', 'Kia', 'via']),
109+
];
110+
}
111+
}
112+
113+
echo new App;
114+
```

0 commit comments

Comments
 (0)