phpreact/component 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpreact/component

Composer 安装命令:

composer require phpreact/component

包简介

create web app via php react like component based

README 文档

README

This project aims to mimic react component in php

it's one of the elegant solution to make component based web app via php

Installation:

composer require phpreact/component

Usage:

To create a component you neet to just extend React\Component class

namespace React\Tag;
use React\Component;

class CustomComponent extends Component{
    function render(){
        return new div(['style'=> 'border:1px solid #eee;border-radius:4px;max-width:500px;padding:5px;margin:10px'],[ 
            new p(['style'=> 'color:red;background:blue'], 'Hello World'), 
            new div('Many div') 
        ]);
    }
}

HTML tags are create via the React Component class all HTML tags are under namespace React\Tag

To register custom html tag you just call a static function registerTag

React\Tag::register('tag1');
React\Tag::register(['newtag1', 'newtag2']); //multiple html tags

To render your app

echo new CustomComponent;

we can now have the ability to mimic reactjs state management.

there some slight difference in apply setState as we need to connect js event setState to php component

Note: make sure the component is wrapped by htmltag (ie: p, div, ...)

class CustomComponent extends Component{
    var $state = ['test' => 1];
    
    function componentDidUpdate($prevState, $currState){} //run only when there's state update
    
    function render(){
        $test = $this->state->test;
        
        return new div(['style'=> 'border:1px solid #eee;border-radius:4px;max-width:500px;padding:5px;margin:10px'],[ 
            new p(['style'=> 'color:red;background:blue'], 'Hello World',), 
            new div('Many div'),
            new button(['onclick'=> "this.setState({test: ".($test+1)."})"], "set my state ($test)") 
        ]); 
    }
}

Sample full example

namespace React\Tag;    
use React\Component;


include_once 'vendor/autoload.php';

//functional Component
function Cars($children = [], $text){
    return new div(['style'=>'display:flex'], array_map(function($v) use($text){ 
        return  new Car(['text'=> $v . $text]); 
    }, $children));
}

function Car($text = ''){
    $state = \React\useState(['test'=> 1]);
    return new div(['onclick'=> 'this.setState(function(state){ return {test: state.test + 1}})','style'=> [
        'color'=> '#fff', 
        'border-style'=>'solid', 
        'border-radius'=> 2,
        'padding'=>5, 
        'border-color'=> 'red', 
        'border-width'=> 2,
        'margin' => 5,
        'background'=> 'brown',
        'width'=> '100%',
        'text-align'=> 'center'
        ]], $text . ' '. $state->test); 
}

class App extends Component{
    function render(){
        return [
            new div(['style'=>'color:red'],'test world'),
            new div(['style'=>'color:red'], 'cool'),
            new Cars(['text'=> ' hello world'],['Volsvagen', 'Kia', 'via']),
        ];
    }
}

echo new App;

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固