首页 \ 问答 \ React:在设置状态之前初始渲染(通过ajax)(React: initial render fires before state is set (via ajax))

React:在设置状态之前初始渲染(通过ajax)(React: initial render fires before state is set (via ajax))

我已经进行了设置,因此HomePage组件为当前登录的用户呈现UserShow 。 例如,如果ID为2的用户登录并访问HomePage页面,它将呈现其UserShow

“普通” UserShow正常工作。 例如,如果您键入/ users / 18,它将正确呈现。 但是,当HomePage呈现它时,它无法正常工作。

我是React的新手(特别是它的生命周期方法),所以我的调试是在各个步骤中发出警报。 我想说最重要的发现是:

  1. currentUserID()正在运行并返回正确的ID
  2. componentDidMount中state.userID的值进行硬编码会使事情正常工作

这两点让我相信Render在被更新state.userID及其(正确的)返回值之前被调用。 更具体的是它在this.currentUserID() ajax调用返回的.success部分之前呈现。 如果是这样,那么在这样的ajax调用完成之前,最好的方法是不进行初始渲染?

我的代码处于意大利面状态 - 这是我第一次使用JavaScript进行前端路由。 我也通过使用用户的电子邮件作为localStorage中的令牌来管理会话 - 我也是JS中的新会话。 请多多包涵。

主页组件:

var HomePage = React.createClass({

getInitialState: function(){
    return{
        didFetchData: false,
        userID: null,
    }
},

componentWillMount: function(){
    newState = this.currentUserID()
    this.setState({userID: newState}) 
    // this.setState({userID: 2})   //hard-coding the value works
},

currentUserID: function(){
    if(App.checkLoggedIn()){
        var email = this.currentUserEmail()
        this.fetchUserID(email)
    }else{
        alert('theres not a logged in user')
    }
},

currentUserEmail: function(){
    return localStorage.getItem('email')
},

fetchUserID: function(email){ //queries a Rails DB using the user's email to return their ID
    $.ajax({
        type: "GET",
        url: "/users/email",
        data: {email: email},
        dataType: 'json',
        success: function(data){
            this.setState({didFetchData: 'true', userID: data.user_id})
        }.bind(this),
        error: function(data){
            alert('error! couldnt fetch user id')
        }
    })
},

render: function(){
    userID = this.state.userID
    return(
        <div>
            <UserShow params={{id: userID}} />
        </div>
    )
}
})

UserShow组件:

var UserShow = React.createClass({

getInitialState: function(){
    return{
        didFetchData: false,
        userName: [],
        userItems: [],
        headerImage: "../users.png"
    }
},

componentDidMount: function(){
    this.fetchData()
},

fetchData: function(){  
    var params = this.props.params.id
    $.ajax({
        type: "GET",
        url: "/users/" + params,
        data: "data",
        dataType: 'json',
        success: function(data){
            this.setState({didFetchData: 'true', userName: data.user_name, userItems: data.items, headerImage: data.photo_url})
        }.bind(this),
        error: function(data){
            alert('error! couldnt load user into user show')
        }
    })
},

render: function(){
    var userItem = this.state.userItems.map(function(item){
        return <UserItemCard name={item.name} key={item.id} id={item.id} description={item.description} photo_url={item.photo_url} />
    })
    return(
        <div>
            <Header img_src={this.state.headerImage} />

            <section className="body-wrapper">
                {userItem}              
            </section>
        </div>
    )
}
})

I've set things up so the HomePage component renders UserShow for the current logged in user. For example, if a user with an ID of 2 is logged in and visits the HomePage page, it will render their UserShow.

The "normal" UserShow works correctly. For example if you type in /users/18, it will properly render. However it's not working when HomePage renders it.

I'm new to React (especially its lifecycle methods), so my debugging has been to throw alerts in at various steps. I'd say the most important findings to share are:

  1. currentUserID( ) is functioning and returns the correct ID
  2. Hard-coding the value of state.userID within componentDidMount causes things to work correctly

These two points lead me to believe that Render is being called before it can update state.userID with its (correct) return value. Even more specific is that it's rendering before the .success portion of the this.currentUserID() ajax call returns. If this is so, what's the best way to go about not doing an initial render until an ajax call like this completes?

My code is in a state of spaghetti - it's my first time doing front-end routing with JavaScript. I'm also managing sessions via using the user's email as the token in localStorage - I'm new to sessions in JS as well. Please bear with me.

HomePage component:

var HomePage = React.createClass({

getInitialState: function(){
    return{
        didFetchData: false,
        userID: null,
    }
},

componentWillMount: function(){
    newState = this.currentUserID()
    this.setState({userID: newState}) 
    // this.setState({userID: 2})   //hard-coding the value works
},

currentUserID: function(){
    if(App.checkLoggedIn()){
        var email = this.currentUserEmail()
        this.fetchUserID(email)
    }else{
        alert('theres not a logged in user')
    }
},

currentUserEmail: function(){
    return localStorage.getItem('email')
},

fetchUserID: function(email){ //queries a Rails DB using the user's email to return their ID
    $.ajax({
        type: "GET",
        url: "/users/email",
        data: {email: email},
        dataType: 'json',
        success: function(data){
            this.setState({didFetchData: 'true', userID: data.user_id})
        }.bind(this),
        error: function(data){
            alert('error! couldnt fetch user id')
        }
    })
},

render: function(){
    userID = this.state.userID
    return(
        <div>
            <UserShow params={{id: userID}} />
        </div>
    )
}
})

UserShow component:

var UserShow = React.createClass({

getInitialState: function(){
    return{
        didFetchData: false,
        userName: [],
        userItems: [],
        headerImage: "../users.png"
    }
},

componentDidMount: function(){
    this.fetchData()
},

fetchData: function(){  
    var params = this.props.params.id
    $.ajax({
        type: "GET",
        url: "/users/" + params,
        data: "data",
        dataType: 'json',
        success: function(data){
            this.setState({didFetchData: 'true', userName: data.user_name, userItems: data.items, headerImage: data.photo_url})
        }.bind(this),
        error: function(data){
            alert('error! couldnt load user into user show')
        }
    })
},

render: function(){
    var userItem = this.state.userItems.map(function(item){
        return <UserItemCard name={item.name} key={item.id} id={item.id} description={item.description} photo_url={item.photo_url} />
    })
    return(
        <div>
            <Header img_src={this.state.headerImage} />

            <section className="body-wrapper">
                {userItem}              
            </section>
        </div>
    )
}
})

原文:https://stackoverflow.com/questions/33538352
更新时间:2023-12-17 17:12

最满意答案

尝试:

"[Mau_con]![Action Date] Between DateValue([Forms]![frmReportFilter]![txtStartDate]) And DateValue([Forms]![frmReportFilter]![txtEndDate])"

要么:

"([Mau_con]![Action Date] Between DateValue([Forms]![frmReportFilter]![txtStartDate]) And DateValue([Forms]![frmReportFilter]![txtEndDate])) And ([Mau_con]![Advisor Name] = [Forms]![frmReportFilter]![cmbAdvNam])"

Try with:

"[Mau_con]![Action Date] Between DateValue([Forms]![frmReportFilter]![txtStartDate]) And DateValue([Forms]![frmReportFilter]![txtEndDate])"

or:

"([Mau_con]![Action Date] Between DateValue([Forms]![frmReportFilter]![txtStartDate]) And DateValue([Forms]![frmReportFilter]![txtEndDate])) And ([Mau_con]![Advisor Name] = [Forms]![frmReportFilter]![cmbAdvNam])"

相关问答

更多
  • 您需要为日期值格式化格式正确的字符串表达式: Dim dateFrom As Date Dim dateTo As Date dateFrom = DateValue(Forms!formOptions!txtDateFrom.Value) dateTo = DateValue(Forms!formOptions!txtDateTo.Value) strCriteria = "[ActionTime] >= #" & Format(dateFrom, "yyyy\/mm\/dd") & "# And [ ...
  • 由于这些行没有任何类型的顺序唯一键号,因此您需要创建一个排名列: 如何在查询中排列记录 您需要确定一次将返回N个行数(N = 10,25,100)。 您需要跟踪用户所处的“页面”以及第一个和最后一个排名的值。 然后,当您为下一页进行调用时,它可能是接下来的N行>或<第一行和最后一行(取决于用户是转到上一页还是下一页)。 我确定有一种方法可以计算最后一页,第一页等。 Since it doesn't appear that you have any type of sequencial unique key ...
  • 在Windows机器上安装PostgreSQL ODBC驱动程序 。 创建一个指向PostgreSQL数据库的DSN。 启动Access会话并使用DSN导入PostgreSQL表。 过去我已经做了几次,发现它很快捷。 使用ODBC驱动程序进行访问会将PostgreSQL列数据类型转换为与Access兼容的类型。 这种方法应该适用于简单的表格。 但是,如果您的设计使用高级PostgreSQL功能,则这些功能可能无法很好地(或完全)转换为Access。 但是,由于您尚未创建数据库,我鼓励您容忍Access表设计 ...
  • 查询中的表未链接。 通过拖放链接查询设计器中的相关列,如关系创建 Tables in your query not linked. Link related colums in query designer by drag-and-drop, like relationship creation
  • 尝试: "[Mau_con]![Action Date] Between DateValue([Forms]![frmReportFilter]![txtStartDate]) And DateValue([Forms]![frmReportFilter]![txtEndDate])" 要么: "([Mau_con]![Action Date] Between DateValue([Forms]![frmReportFilter]![txtStartDate]) And DateValue([Forms] ...
  • 在MS Access中,您可以使用内联IIF(条件,true,false) 在你的情况下。 SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Versicherungsnummer, IIF(ISNULL(Laufzettel.EPolicierungVSL),1,0) AS dunkel , Laufzettel.AEingangDatenstromZWorkflow , Laufzettel.B ...
  • 哦伙计。 你意识到这有多糟糕? 遗憾的是,“不知道如何使用数据库”的风格。 是时候阅读有关数据类型的文档了。 好的,我们走了: TaskDate(格式为备忘录) 是的,然而这应该被公开鞭打。 首先,这是一个TEXT字段,第二个是LONG文本,它们的日期永远不会。 两级失败。 如果格式为Memo,则为文本,因此“between”比较文本。 '01 / 01/2012'和'12 / 31 / 2012'` 这是从左到右。 即第一个字母0或1,第二个字母1或2,第4个字母在0和3之间,第5个字母在1和1之间。继续 ...
  • 你可以接受这个想法并玩它。 将其插入模块 Option Explicit Dim g_ManagerReportFilterEnabled As Boolean Dim g_ManagerReportFilter As String Public Function IsManagerReportFilterEnabled() As Boolean IsManagerReportFilterEnabled = g_ManagerReportFilterEnabled End Function P ...

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)