小编Phi*_*nin的帖子

用计算字段反应状态

我有一个反应组件,它具有属性和状态。一些状态字段包含输入数据(从输入控件中提升),但状态中的某些字段也必须根据当前状态和属性进行计算:

在此处输入图片说明

问题:更新状态的计算字段的最佳方法是什么(基于状态和道具的其他字段)?

这样做的丑陋方式:

componentDidUpdate(){
    this.setState({calculatedField:calculate(this.props,this.state)})) 
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我将获得无限循环的更新,或者在最佳情况下(如果使用PureComponent,则是)双重渲染调用。

到目前为止,我发现的最佳解决方案(但仍然很丑):创建一个calculated处于状态的对象,该对象包含计算所得的字段并在componentWillUpdate中进行更新,避免使用setState:

componentWillUpdate(nextProps,nextState){
   nextState.calculated.field1=f(nextProps,nextState)
}
Run Code Online (Sandbox Code Playgroud)

componentDidUpdate(){
    this.setState({calculatedField:calculate(this.props,this.state)})) 
}
Run Code Online (Sandbox Code Playgroud)
componentWillUpdate(nextProps,nextState){
   nextState.calculated.field1=f(nextProps,nextState)
}
Run Code Online (Sandbox Code Playgroud)

这也是丑陋的解决方案,不建议React避免setState来改变状态。那么什么是正确的解决方案呢?

注意:

在我的真实应用程序中,由于渲染实际上是复杂的对象,因此无法在渲染过程中每次都重新计算f(a,b),因此我需要以某种方式对其进行缓存,并且最好的方式是处于状态。

javascript state reactjs

4
推荐指数
2
解决办法
5495
查看次数

Python 3.6类型:在赋值之前使用变量

类型/提示/分配的新方法很酷,但我不知道如何使这么简单的事情起作用:

class MyContainer:
    def addMyItem(self, item:MyItem):
        pass

class MyItem:
    def __init__(self, container:MyContainer):
        pass
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误:Using variable 'MyItem' before assignment.到目前为止我发现的最好但超级丑陋的解决方法是:

class MyContainer:
    def addMyItem(self, untypeditem):
        item:MyItem=untypeditem
        pass

class MyItem:
    def __init__(self, container:MyContainer):
        pass
Run Code Online (Sandbox Code Playgroud)

请告诉我,#1原则的语言可以Beautiful is better than ugly更好地解决这个常见的打字问题

python types python-3.x

3
推荐指数
1
解决办法
547
查看次数

如果要处理按钮点击事件,则在Page.Load期间识别

我有ASPX网页,上面有一个按钮.用户单击此按钮后,将请求提交给服务器并执行按钮单击事件处理程序.

我有一些逻辑必须驻留在Page.Load上,但是这个逻辑取决于是否通过按钮点击提交了请求.基于页面生命周期事件处理程序在页面加载后执行.

问题:如何在页面加载中找出页面加载后要执行的事件处理程序?

.net c# asp.net event-handling

2
推荐指数
1
解决办法
5873
查看次数

与现有文件夹冲突的MVC路由导致404

我确实看到了类似问题的答案,但没有一个适用于这种情况.

我有一个名为"设置"的注册区域的MVC4应用程序,带有一个"管理"控制器.我的网络项目中还有一个名为"设置"的源文件夹,因此文件夹结构如下:

+ WebProjectFolder
     + Areas
          +Settings
               +Controllers
                    ManageController.cs
               + SettingsAreaRegistration.cs
               ...
     + Settings
          + SomeClasses.cs
Run Code Online (Sandbox Code Playgroud)

ManageController非常简单:

public class ManageController : Controller
{
    public ActionResult Index()
    {
        return Content("WORKS FINE!");
    }
}
Run Code Online (Sandbox Code Playgroud)

区域注册也很简单:

public class SettingsAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Settings";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.Routes.IgnoreRoute("Settings/{*pathinfo}");
        context.Routes.RouteExistingFiles = false;

        context.MapRoute(
            "Settings_default",
            "Settings/{controller}/{action}/{id}",
            new {area = context.AreaName, controller="Manage", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

http://mysite/settings …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-4

2
推荐指数
1
解决办法
933
查看次数