我允许请求您的意见。实际上,我想到了一种改变反应组件(DOM)结构的方法。例如,我有一个 Page1 组件。 Normaly,我们有这个:
import React from "react";
class Page1 extends React.Component
{
...
render()
{
return (
/*Here JSX DOM*/
);
}
}
要更改此页面的DOM(根据模板),我认为:
import React from "react";
class Page1 extends React.Component
{
...
render()
{
return Template.get("page1", this.props);
}
}
模板类的 get 方法根据config中的模板定义返回JSX DOM:
{
template: "base"
}
模板类:
import {template} from "config" // Name of a template
class Template
{
static get(componentName, props)
{
var templatePath = path.join("templates", template);
return require(templatePath)[componentName](props);
}
}
在templates文件夹中,我们可以使用以下结构:
templates
|
|____base
| |
| |__index.js
| |
| |__pag1.tpl.jsx
|
|____other
|
|__index.js
|
|__page1.tpl.jsx
基本模板的 page1.tpl.jsx 如下所示:
import React from "react";
export default function(props) {
return (
<div>Hello World !!!</div>
);
}
我对你的意见感兴趣。
答案 0 :(得分:1)
在我看来,反应组件应该尽可能模块化,因此它们就像一个模板。
所以,你的想法很好,但你应该使用react组件作为模板,而不是普通的es6类。
React组件可以自动检查(必需)属性是否定义return Template.get("page1", this.props);
。此外,语法将从
return <Page1 {...this.props} />;
到
public class Employee extends javax.swing.JFrame {
// Variables declaration
private javax.swing.JTextArea dispAREA;
private javax.swing.JTextField dispID;
private javax.swing.JTextField dispNAME;
private javax.swing.JTextField dispSAL;
private javax.swing.JButton findBUTTON;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
public Employee() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
findBUTTON = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
dispID = new javax.swing.JTextField();
dispNAME = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
dispAREA = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
dispSAL = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
findBUTTON.setText("Find");
findBUTTON.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
findBUTTONActionPerformed(evt);
}
});
jLabel1.setText("Employee ID");
jLabel2.setText("Name");
dispAREA.setColumns(20);
dispAREA.setRows(5);
jScrollPane1.setViewportView(dispAREA);
jLabel3.setText("Salary");
dispSAL.setText(" ");
}// </editor-fold>
private void findBUTTONActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName( "com.mysql.jdbc.Driver" );
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Emp","root","");
Statement statement = con.createStatement();
String query = "SELECT * FROM EmpInfo ";
query +="WHERE EmpInfo = "+this.dispID.getText();
if(this.dispNAME.getText().length() != 0){
query +=" and Name = '"+this.dispNAME.getText()+"'";
}
if(this.dispSAL.getText().length() != 0){
query +=" and Salary = '"+this.dispSAL.getText()+"'";
}
//dispAREA.append("\nYour Query is :"+query+"\n");
ResultSet rs = statement.executeQuery(query);
boolean moreRecords = rs.next();
if(!moreRecords){
dispAREA.append("Sorry,no match!");
this.dispID.setText("");
}
do{
this.dispNAME.setText(rs.getString(2));
this.dispSAL.setText(rs.getString(3));
this.dispAREA.setText(rs.getString(4));
}while(rs.next());
statement.close();
con.close();
}
catch(SQLException e){
dispAREA.append("");
//outputjTextArea.append(e.getStackTrace().toString());
//e.printStackTrace();
}catch(ClassNotFoundException e){
//e.printStackTrace();
dispAREA.append("");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Employee().setVisible(true);
}
});
}
在我看来,更容易阅读。