相关疑难解决方法(0)

为什么调用setState方法不会立即改变状态?

好的,我会尽快做到这一点,因为它应该是一个简单的解决方案......

我读了很多类似的问题,答案似乎很明显.从来没有什么我不得不抬头看!但是......我有一个错误,我无法理解如何解决或为什么会发生这种情况.

如下:

class NightlifeTypes extends Component {
constructor(props) {
    super(props);

    this.state = {
        barClubLounge: false,
        seeTheTown: true,
        eventsEntertainment: true,
        familyFriendlyOnly: false
    }
    this.handleOnChange = this.handleOnChange.bind(this);
}

handleOnChange = (event) => {   
    if(event.target.className == "barClubLounge") {
        this.setState({barClubLounge: event.target.checked});
        console.log(event.target.checked)
        console.log(this.state.barClubLounge)
    }
}

render() {
    return (
        <input className="barClubLounge" type='checkbox' onChange={this.handleOnChange} checked={this.state.barClubLounge}/>
    )
}
Run Code Online (Sandbox Code Playgroud)

更多代码围绕着这个,但这就是我的问题所在.应该工作吧?

我也试过这个:

handleOnChange = (event) => {   
if(event.target.className == "barClubLounge") {
    this.setState({barClubLounge: !this.state.barClubLounge});
    console.log(event.target.checked)
    console.log(this.state.barClubLounge)
}
Run Code Online (Sandbox Code Playgroud)

所以我有两个console.log(),都应该是一样的.我确实将状态设置为与event.target.checked它上面的行相同!

但它总是与它应该的相反.

我使用时也一样!this.state.barClubLounge; 如果它开始为假,在我第一次点击它仍然是假的,即使复选框是否被选中是基于状态!!

这是一个疯狂的悖论,我不知道最新情况,请帮忙!

html javascript event-handling reactjs

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

React react-router-dom将props传递给组件

我需要使用路由器将props传递给组件.这是我的代码:

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import AppBarTop from './appbar/AppBarTop';

import Login from '../pages/login/Login';
import {BrowserRouter as Router, Route} from 'react-router-dom';


class App extends Component {

    render() {

        const { isAuthenticated } = this.props;

        return (
            <Router>
                <div>
                    <AppBarTop isAuthenticated={isAuthenticated} />
                    <div className="content">
                        <Route path="/login" isAuthenticated={isAuthenticated} component={Login} />
                    </div>
                </div>
            </Router>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,isAuthenticated我要传递给Login组件的prop.

class Login extends Component {

    constructor(props) {
        super(props);
        console.log(props);
    }

    render() {
        return (
            <LoginForm /> …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router

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

以编程方式在React-Router v4中导航

在完成一些验证后,如何移动到新页面React Router V4?我有这样的事情:

export class WelcomeForm extends Component {

    handleSubmit = (e) => {
        e.preventDefault()

        if(this.validateForm())
            // send to '/life'

    }
    render() {
        return (
            <form className="WelcomeForm" onSubmit={this.handleSubmit}>
                <input className="minutes" type="number" value={this.state.minutes} onChange={ (e) => this.handleChanges(e, "minutes")}/>
            </form>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将用户发送到另一条路线.我看了一下Redirect,但似乎它会删除我不想要的历史记录中的当前页面.

reactjs react-router react-router-v4

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

React - 如何在合成事件回收之前触发preventDefault()?

我在 React 组件中有以下函数,该函数在单击链接时运行:

onClick(e, { name }) {
    e.stopPropagation();

    const doIt = await checkSomething();

    if (doIt) {
        e.preventDefault();
        doSomethingElse();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,到了时间,e.preventDefault()合成事件就被回收了。那么,当需要一些逻辑来计算此决定时以及在回收合成事件之前,如何告诉浏览器在点击链接时不要点击该链接?

preventdefault reactjs

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