Hi, I'm starting to study the reaction. Therefore, after understanding the basics, I begin to work with connecting to the database using reactjs. In the Im code trying to get userId and Password, establish a connection to the database and try to list the tables available in the database. In Login.js, I created a form (userId and Password) when I click the login button. I will make a connection and execute the Show Table query to list all the tables in the database and go to the Table.js page where I am trying to list the available tables. Right now I can connect to the database, but I could not display the tables in Table.js , therefore, how to display the list of tables in the Table.js file, because I placed my database connection and request inside the button event in Login.js. Also, is it possible to declare a global variable and access it through other js files. Any help would be great, thanks.
Login.js
import React from 'react'; import TableContent from './tables'; class Login extends React.Component{ constructor(){ super(); this.state={ showComponent : false, }; // this.buttonClick = this.buttonClick.bind(this); } buttonClick(event){ event.preventDefault(); this.setState({ showComponent: true, }) var db = require('@dataBase/dynamoConnect')({ "UserId": "XXXXXXXXXXXXXXXXXXXXXXXX", "Password": "YYYYYYYYYYYYYYY", "region": "ZZZZZZZZZZ" }); db.query("SHOW TABLES",(err,data)=>{ const tableList = data; console.log(tableList); }) } render(){ return( <div> <form> <label>User Id :</label> <input type="text" className="test"/> <br/> <label>Password :</label> <input type="text" className="test" /> <button onClick={this.buttonClick.bind(this)} className="connect" > Login</button> </form> {this.state.showComponent && <TableContent />} </div> ) } } export default Login;
Table.js
import React from 'react'; class TableContent extends React.Component { constructor() { super(); this.state = { showComponent: false, }; this.buttonClick = this.buttonClick.bind(this); } buttonClick(event) { event.preventDefault(); this.setState({ showComponent: true, }) } render() { return ( <div> <form> <div id="first"> <label> Table </label> <br /> //Display the tables from DB here <select name="sometext" multiple="multiple" > <option>Table1</option> <option>Table2</option> <option>Table3</option> <option>Table4</option> <option>Table5</option> </select> </div> <div id="second"> <label> SQL </label> <br/> <textarea rows="4" cols="50">SQL </textarea> </div> <button onClick={this.buttonClick.bind(this)} > Execute </button> <div id="third" > {this.state.showComponent && <SampleTable />} </div> </form> </div> ) } } export default TableContent;
source share