小编Hie*_*ero的帖子

在React中切换类

我正在使用一个项目,我有一个菜单按钮.

<a ref="btn" href="#" className="btn-menu show-on-small"><i></i></a>
Run Code Online (Sandbox Code Playgroud)

Sidenav组件如:

<Sidenav ref="menu" />
Run Code Online (Sandbox Code Playgroud)

我编写了以下代码来切换菜单:

class Header extends React.Component {

    constructor(props){
        super(props);
        this.toggleSidenav = this.toggleSidenav.bind(this);
    }

    render() {
        return (
          <div className="header">
            <i className="border hide-on-small-and-down"></i>
            <div className="container">
          <a ref="btn" href="#" className="btn-menu show-on-small"><i></i></a>
          <Menu className="menu hide-on-small-and-down"/>
          <Sidenav />
        </div>
          </div>
        )
    }

    toggleSidenav() {
        this.refs.btn.classList.toggle('btn-menu-open');
    }

    componentDidMount() {
        this.refs.btn.addEventListener('click', this.toggleSidenav);
    }

    componentWillUnmount() {
        this.refs.btn.removeEventListener('click', this.toggleSidenav);
    }
}
Run Code Online (Sandbox Code Playgroud)

事情是,这this.refs.sidenav不是一个DOM元素,我不能在他身上添加一个类.

有人可以解释我如何Sidenav在我的按钮上切换组件上的类吗?

javascript reactjs

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

反应路由器全局标头

我刚开始学习React,我正在尝试创建一个SPA博客,它有一个全球定位的固定标题.

import React from 'react';
import { render } from 'react-dom';
// import other components here

    render((
      <Router history={browserHistory}>
        <Route path="/" component={Home} />
        <Route path="/About" component={About} />
        <Route path="/Contact" component={Contact} />
        <Route path="*" component={Error} />
      </Router>
    ), document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)

所以,每条路线都有相同的标题,从我的角度背景,我会在ui-view之外使用标题.

在每个单独的页面组件中导入标题组件是一个很好的做法,还是可以在我的标题组件上添加标题组件<Router><myHeader/><otherRoutes/></Router>

更新:

我想用这样的东西:

路由组件,我在其中定义路由:

class Routes extends React.Component {
    render() {
        return (
            <Router history={browserHistory}>
                <IndexRoute component={Home} />
                <Route path="/studio" component={Studio} />
                <Route path="/work" component={Work} />
                <Route path="*" component={Home} />
            </Router>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在主要的Index.js文件中,我想渲染类似于:

import Routes from './components/Routes'; …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router

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

如何在插入违反唯一索引的MongoDB文档时捕获错误?

我正在构建一个MEAN应用程序.

这是我的用户名架构,用户名应该是唯一的.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

module.exports = mongoose.model('User', new Schema({ 
    username: { type: String, unique: true }
}));
Run Code Online (Sandbox Code Playgroud)

在我的帖子路线上,我像这样保存用户:

app.post('/authenticate', function(req, res) {
        var user = new User({
            username: req.body.username
        });

        user.save(function(err) {
            if (err) throw err;

            res.json({
                success: true
            });

        });
    })
Run Code Online (Sandbox Code Playgroud)

如果我再次使用相同的用户名发布,我会收到此错误:

MongoError:insertDocument ::由:: 11000引起的E11000重复键错误索引:

有人可以解释如何发送json而不是错误 { succes: false, message: 'User already exist!' }

注意:在我发布用户后,我将自动进行身份验证,不需要密码或其他内容.

javascript mean mongoose mongodb express

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

我在react-native和redux中处理API调用的地方

我正在使用react-native+ redux+ 构建Android应用immutable

我像这样构建了我的应用程序

/src
 -/components
 -/actions (where I have action types and actions)
 -/reducers
 -/api (I'm not sure if I'm doing this right)
Run Code Online (Sandbox Code Playgroud)

好的,所以我有一个动作,我需要用这样的fetch进行API调用:

import * as types from './casesTypes'

export function getCases() {
    return {
      type: types.GET_CASES
    }
}
Run Code Online (Sandbox Code Playgroud)

这是减速器的情况:

const initialState = fromJS({
  isLoading: false,
  cases: null
})
export default function cases(state = initialState, action = {}) {

    switch(action.type) {

        case types.REQUEST_CASES:
          return state.set('isLoading', true)

        case types.RECIVE
          return state.set('cases', //set here the array …
Run Code Online (Sandbox Code Playgroud)

reactjs react-native redux react-redux

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

Flexbox与底部对齐

我有以下布局http://jsbin.com/joyetaqase/1/edit?html,css,output

<div class="row">

    <div class="col">
      <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
      <p>my footer</p>
    </div>

     <div class="col">
      <h2>Lorem Ipsum.</h2>
      <p>my footer</p>
    </div>

  </div>
Run Code Online (Sandbox Code Playgroud)

使用flexbox我试图为.coldiv 创建相同的高度和相同的宽度.

我的问题是:我怎么能把它<p>贴在盒子的底部?

布局应如下所示:

在此输入图像描述

我知道我可以做<p>绝对和使用,bottom:0;但我想用flexbox实现这一点,是否可能?

谁能解释一下?

html css css3 flexbox

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

Highcharts突出显示两条线之间的区域

我有以下图表,其中有两行

http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-basic/

series: [{
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
Run Code Online (Sandbox Code Playgroud)

我想要实现的目标是突出东京和伦敦之间的空间,这可能与Highcharts有关吗?

(与http://jsfiddle.net/highcharts/WRcj9/类似)

有人可以解释我如何实现这一点或知道更好的方法来做到这一点?

javascript highcharts

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

找不到 .zshrc 文件

最近我安装了其他人的 .dotfiles 并覆盖了我的配置。

.zshrc问题是我想为我的 ZSH 使用自定义设置,但我在里面找不到~/,我有其他 .zshrc. 文件,但编辑这些文件都不起作用。

在此输入图像描述

另外,我安装的点文件位于“~/.dotfiles”中,但似乎这些文件都不包含配置。

在此输入图像描述

有人可以解释一下现在如何编辑 ZSH,我可以恢复旧的 .zshrc 吗?

macos zsh zshrc oh-my-zsh

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

react-native fetch返回奇数响应

我正在使用fetch从这样的API中获取一些东西:

fetch('http://facebook.github.io/react-native/movies.json')
        .then(
            data => console.log(data),
            error => console.log(error)
        )
Run Code Online (Sandbox Code Playgroud)

但我得到的是以下对象,而不是实际数据

_bodyBlob: Blob
_bodyInit: Blob
headers: Headers
ok: true
status: 200
statusText: undefined
type: "default"
url: "http://facebook.github.io/react-native/movies.json"
Run Code Online (Sandbox Code Playgroud)

有人可以解释我哪里错了?我做错了什么?

javascript fetch react-native

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

Highcharts在X轴上显示series.name

我试图在X 轴上显示每个系列名称 http://jsfiddle.net/Jr79Y/9/ 在此输入图像描述

series: [{
            name: 'Test',
            data: [20]
        }, {
            name: 'Test',
            data: [20]
        }, {
            name: 'Test',
            data: [40]
        }]
Run Code Online (Sandbox Code Playgroud)

有人知道怎么做吗?

javascript charts svg highcharts

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

添加函数到$(jQuery)

也许这对你们中的一些人来说很简单但是如何添加

$.functionName(args)

我已经阅读并试图jQuery.extend$.fn:

$.fn.alertSometing = function(param) {
  alert(param);
};
Run Code Online (Sandbox Code Playgroud)

但是当我跑步的时候

  $('.button').on('click', $.alertSometing('something'));
Run Code Online (Sandbox Code Playgroud)

返回此错误:

未捕获的TypeError:$ .alertSometing不是函数(匿名函数)

这是我的代码:http://jsbin.com/kuxomofoka/edit?html,js,console,output

有人可以解释我如何实现或解释我做错了什么?

谢谢!

javascript jquery

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