How to pass the data from one js file to another react.js file

9.6k views Asked by At

I want to pass the data from one js file to another js file here is my code:(react.js)

<script type="text/jsx">
/** @jsx React.DOM */
var APP=
React.createClass({
    getInitialState:function(){
        return{
            result:[]
        };
    },
    componentDidMount: function() {
        alert("DidMount");
        var data = getData();
            alert("final")
    },
        render: function () {
        alert("render");
             return(
                <table className="table table-bordered">
                <tr>
                <th>Name</th>
                <th>Class</th>
                <th>D.O.B</th>
                </tr>
        </table>
)
        }
    })
React.renderComponent(
        <APP/>,
        document.getElementById('root'));

and my js file is :

    getData1=function() {
    alert("initial")
    var Connection = require('tedious').Connection;
    var  Request = require('tedious').Request;
    var config = {
        userName: 'zxzx',
        password: '******',
        server: 'xxxxxxx',// You can use 'localhost\\instance' to connect to named instance
        options: {
            database: 'xxxx',
            rowCollectionOnRequestCompletion: true,
            useColumnNames : true
        }
    };
    var connection = new Connection(config);
    connection.on('connect', function (err) {
        if (err) {
            console.log(err)
        }
        request = new Request("SELECT * FROM XXXXX", function (err, rowCount, rows) {
            if (err) {
                //console.log(err);
                callback(err, null);
            } else {
                console.log(+'rows Count: ' + rowCount);
                console.log(JSON.stringify(rows));
                var datas = JSON.stringify(rows);
                alert(datas)
                alert("rows")
                return datas;
            }
            connection.close();
        });
        connection.execSql(request);
    });
}

what i want is that i need to return the data when calling the function in react, what happening here is that in my console it shows the data, but in react the data is coming as empty, i.e the data is not returned correctly. Can any one give solution for it?

1

There are 1 answers

0
Chris Hawkes On

This is a common question, assuming the react file you're trying use is already loaded prior to your code that is trying to use it, then it would work just fine.

As far as having one component communicate with another component you will need to review this documentation.

https://facebook.github.io/react/tips/communicate-between-components.html

When components need to listen for global events etc... just setup an event and have your component subscribe to it.

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events