【问题标题】:How to pass the data from one js file to another react.js file如何将数据从一个 js 文件传递​​到另一个 react.js 文件
【发布时间】:2015-09-07 19:03:36
【问题描述】:

我想将数据从一个 js 文件传递​​到另一个 js 文件,这是我的代码:(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'));

我的 js 文件是:

    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);
    });
}

我想要的是我需要在调用函数时返回数据反应,这里发生的是在我的控制台中它显示数据,但反应数据是空的,即没有返回数据正确。谁能给出解决方案?

【问题讨论】:

    标签: reactjs nodes electron


    【解决方案1】:

    这是一个常见问题,假设您尝试使用的 react 文件在您尝试使用它的代码之前已经加载,那么它就可以正常工作。

    就让一个组件与另一个组件通信而言,您需要查看此文档。

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

    当组件需要监听全局事件等时...只需设置一个事件并让您的组件订阅它。

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

    【讨论】:

      猜你喜欢
      • 2020-07-10
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      相关资源
      最近更新 更多