小编l2s*_*ver的帖子

React-Redux:所有组件状态都应保存在Redux Store中

说我有一个简单的切换:

单击按钮时,"颜色"组件在红色和蓝色之间变化

通过做这样的事情,我可能会达到这个结果.

index.js

Button: onClick={()=>{dispatch(changeColor())}}
Color: this.props.color ? blue : red
Run Code Online (Sandbox Code Playgroud)

container.js

connect(mapStateToProps)(indexPage)
Run Code Online (Sandbox Code Playgroud)

action_creator.js

function changeColor(){
 return {type: 'CHANGE_COLOR'}
}
Run Code Online (Sandbox Code Playgroud)

reducer.js

switch(){
case 'CHANGE_COLOR':
return {color: true}
Run Code Online (Sandbox Code Playgroud)

但这是一个很多代码的地狱,我可以用jQuery,一些类和一些css在5秒内完成一些代码.

所以我想我真正想问的是,我在这里做错了什么?

javascript reactjs redux

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

在redux thunk dispatch之后从商店返回承诺

//为了清晰而编辑

我正在尝试用redux thunk链接调度

function simple_action(){
  return {type: "SIMPLE_ACTION"}
}

export function async_action(){
  return function(dispatch, getState){
    return dispatch(simple_action).then(()=>{...});
  }
}
Run Code Online (Sandbox Code Playgroud)

如何让调度从商店返回承诺?

进一步来说:

我可能只是在这里不了解一些东西,但是在所有的例子中redux-thunk,它们都会调用一个单独的异步事件(如fetch),这显然会返回一个Promise.

我特别想要的是当我向商店发送一个动作时:如何确保商店在上述功能发生任何其他事件之前完全处理了该动作action_creator().

理想情况下,我希望商店能够返回某种承诺,但我不明白这是怎么回事?

javascript redux redux-thunk

41
推荐指数
3
解决办法
4万
查看次数

护照js缺少证件

现在已经工作了几个小时,非常令人沮丧......

router.post('/',
passport.authenticate('local-signup', function(err, user, info) {
    console.log(err);
}), function(req, res){
    console.log(req);
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ a: 1 }));
});
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我使用了console.log输出{ message: 'Missing credentials' },这让我相信身体解析器没有正确解析正文消息.当我使用这条路线时......

router.post('/',
    function(req, res){
        console.log(req.body);
        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify({ a: 1 }));
    });
Run Code Online (Sandbox Code Playgroud)

当我使用时console.log,输出为{password: 'password', email: 'email@email.com'},表示req.body变量设置正确且可用.

app.js

var express = require('express');
var app = express();
var routes = require("./config/routes.config");
var models = require("./config/models.config");
var session = require('express-session');
var bodyParser = require('body-parser');

models.forEach(function(model){
    GLOBAL[model] = require('./models/'+model+".model");
});
var …
Run Code Online (Sandbox Code Playgroud)

passport-local passport.js

17
推荐指数
3
解决办法
2万
查看次数

react router获取完整的当前路径名称

是否有一种简单的方法来返回当前的路由器地址.

IE浏览器,如果我在页面上,我只想根据反应路由器查看我所在的页面.

因此,localhost/admin/users将返回admin/users

显然,我可以通过解析位置获得相同的结果,但我想知道反应路由器是否提供了一个简单的机制来执行此操作,就像它提供params道具一样?

reactjs react-router

14
推荐指数
6
解决办法
5万
查看次数

pandoc将带样式表的html转换为docx

我一直在敲打这个问题几个小时,我确信解决方案很简单,或者根本不存在.

我正在尝试将html文件转换为docx!

<!DOCTYPE html>
<html>
<head>
<style>
body {
    background-color: #d0e4fe;
}

h1 {
    color: orange;
    text-align: center;
}

p {
    font-family: "Times New Roman";
    font-size: 20px;
}
</style>
</head>
<body>

<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我可以转换它没有问题,但我不能让风格坚持下去.

pandoc -s myfile.html -o test64.docx
pandoc -s -c myfile.css myfile.html -o test64.docx
Run Code Online (Sandbox Code Playgroud)

请救救我

html converter docx pandoc

11
推荐指数
2
解决办法
5213
查看次数

Rails ActiveRecord 查询 pluck select sum

我有一个复杂的查询,用于计算对象的关系特征的权重总和(对于 PosgreSQL)

Object.joins(:object_traits).where(object_trait: {name: [list_of_names]}).select("sum(object_traits.weight) as sum_weight, #{other direct object traits}").group("#{other direct object traits}").order('weight_sum')
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想提取每个对象的权重总和

activerecord sum ruby-on-rails

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

reactjs尝试捕获渲染不捕获子错误

我试图将错误捕获添加到组件的渲染功能。当我在实际的render函数中引发错误时,这种方法很好用,但是如果组件的子代中存在错误,则try不会捕获错误(或者它们被子组件错误处理程序拦截,我不确定?)

无论如何有强迫错误的父母。

const SimpleComponent = React.createClass({
    render: function(){
        try{
            throw 'new error'
            return <div>{this.props.children}</div>
        }catch(e){
            console.log('error', e);        
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

以上作品

const SimpleComponent = React.createClass({
    render: function(){
        try{
            return <div>{this.props.children}</div>
        }catch(e){
            console.log('error', e);        
        }
    }
})

const ChildComponent = React.createClass({
    render: function(){
        throw 'child error'
    }
})

<SimpleComponent>
    <ChildComponent />
</SimpleComponent>
Run Code Online (Sandbox Code Playgroud)

上面没有抓住

error-handling reactjs

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

Graphql和往返.这只是一个ios app问题吗?

我正在再看看graphql,我试图理解为什么保存往返行程对开发人员有利.提出要求的费用如此昂贵?我来自网络开发背景.

让我们将标准的rest api与graphql api进行比较.

我需要检索用户的个人信息和他们的朋友列表.传统的休息api可能需要2个电话,一个用于获取个人信息,一个用于获取朋友.

使用graphql,我可以通过一个请求获得相同的结果.

但作为前端开发人员,我希望我的页面有最短的停滞期.我想尽可能快地呈现页面的一部分,而不是等待我需要的所有信息然后再渲染页面.

现在从我的理解,graphql部分创建,以解决移动应用程序api问题.有没有关于ios应用程序的东西,它使得一次加载所有数据更有利,而不是并行请求?或者还有其他我想念的东西?

api rest ios graphql

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