小编Igo*_*Vuk的帖子

MobX 自动运行和构造函数内的反应

自动运行反应必须在构造函数内部才能工作吗?我可以在没有构造函数的情况下编写这个简单的示例吗?

另外,我在自动运行中的代码可以正常运行,但如果我将其更改为console.log(this.expenses)它就不起作用。这是为什么?

import { observable, action, computed, useStrict, autorun, reaction } from 'mobx'
useStrict(true)

class ExpensesStore {

  @observable.shallow expenses = []

  @action addExpense = (expense) => {
   this.expenses.push(expense)
  }

  @computed get getExpense() {
    if(this.expenses.length > 0) {
      return `This is computed from ${this.expenses[0] + this.expenses[1]}`
    }

  }


  constructor() {
    autorun(() => {
      console.log(`${this.expenses}`)
    })

    reaction(
      ()=>this.expenses.map(expense => expense), expense => console.log(expense)
    )
  }


}

const store = window.store= new ExpensesStore() …
Run Code Online (Sandbox Code Playgroud)

javascript autorun reactjs mobx

5
推荐指数
1
解决办法
6491
查看次数

服务器端渲染和媒体查询

我有一个根据屏幕大小呈现不同组件的应用程序。它使用媒体查询。

我也在使用服务器端渲染。在服务器端没有媒体查询,所以它最终呈现服务器端的所有内容,这可能导致与客户端不匹配。如何在 SSR 时检测屏幕大小并仅渲染所需的组件?

media-queries reactjs server-side-rendering

5
推荐指数
0
解决办法
244
查看次数

如果(值=== true)不起作用

有人可以向我解释为什么这不起作用?值为true,它是布尔值,如果我检查它就像我通常做的那样(value){}; 有用.为什么不喜欢这个?

function updateRecords(value) {

  console.log(!!(value));        // true
  console.log(typeof(!!(value)));    //boolean

    if (value === true) {
        alert("success");
      }
 }

    updateRecords("Take a Chance on Me");
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
1
解决办法
7524
查看次数

axios doesn't return response and error separately

I have a React component. Inside that component I have a function onFormSubmit that calls function from another component. This other function is making POST request with axios. I would like to return if POST request is true a response into first function or error if not. What is happening now is that my 'SUCCESS RESPONSE' console.log is always triggered, even then there is an error in axios POST request. If there is an error then just 'ERROR RESPONSE' console.log …

javascript promise reactjs axios

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

robots.txt 并禁止绝对路径 URL

我正在使用 Heroku 管道。因此,当我推送应用程序时,它会被推送到暂存应用程序

https://appname.herokuapp.com/
Run Code Online (Sandbox Code Playgroud)

如果一切正确,我就会将该应用程序推广到生产环境。没有新的构建过程。这是第一次构建用于登台的同一个应用程序。

https://appname.com/
Run Code Online (Sandbox Code Playgroud)

问题是这会导致重复内容的问题。站点是彼此的克隆。一模一样。我想从 Google 索引和搜索引擎中排除暂存应用程序。

我想到的一种方法是使用robots.txt文件。

为了这个工作我应该这样写

User-agent: *
Disallow: https://appname.herokuapp.com/
Run Code Online (Sandbox Code Playgroud)

使用绝对路径,因为该文件将位于暂存和生产应用程序的服务器上,我只想从 Google 索引中删除暂存应用程序,而不是触及生产应用程序。

这是正确的做法吗?

seo robots.txt heroku noindex

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