简体   繁体   English

反应状态不重新呈现

[英]React state not rerendering

I have the react component from below and the problem is that it's not automatically rendering when the onClick event is detected (triggering the handleClick function). 我有下面的react组件,问题是当检测到onClick事件(触发handleClick函数)时,它不会自动呈现。

If I write this.forceUpdate() than it's working fine. 如果我写this.forceUpdate()则工作正常。 This shouldn't be normal, it should be working without it. 这不应该是正常的,没有它应该可以正常工作。

import React from 'react';
import Loading from './Loading';

import CameraStore from "../stores/CameraStore";
import * as CameraActions from "../actions/CameraActions";

class LocationCameras extends React.Component {
    constructor(props){
        super(props);  

        this.state = {
            updating: false
        };

        this.handleClick = this.handleClick.bind(this)
    }

    handleSwitch(){

    }

    handleClick(){

        if(this.state.updating == false){
           this.state.updating = true;
        } else if (this.state.updating == true) {
            this.state.updating = false;
        }

        console.log(this.state.updating);
        this.forceUpdate();

    }

    render(){
        var that = this;
        return(
            <div className="cameras">
                <div className="panel panel-default">
                    <div className="panel-heading">
                        Client:  {this.props.locationName}
                        <span className='camera--make-updates' onClick={this.handleClick} >
                            Update
                        </span>
                    </div>
                    <div className="panel-body">
                        {
                            this.props.cameras.map(function(camera,index){

                                if (camera.disabled  !== "1"){
                                    var statusClassName;
                                    var checkStatus;
                                    if (camera.status == 1){
                                        var statusClassName = 'location--camera active'; 
                                       checkStatus = true;
                                    }
                                    else {
                                        var statusClassName = 'location--camera disabled';
                                       checkStatus = false;
                                    }

                                    var handleSwitch = function(event){
                                        var cameraId = camera.cameraId; 
                                        that.handleSwitch(event, cameraId, index);

                                    }

                                            return (

                                                <div className={statusClassName} key={camera.cameraId}>
                                                   {
                                                    that.state.updating == true ? (

                                                        <div className="camera--switch">

                                                           <input type="checkbox" id={'switch' + camera.cameraId} defaultChecked={checkStatus} onChange={handleSwitch} className="switch--input"/>
                                                           <label htmlFor={'switch' + camera.cameraId} className="switch--label"></label>

                                                        </div>

                                                        ) : (

                                                            <div className="camera--status"></div>

                                                        )


                                                    }

                                                    <div className="camera--name-id">
                                                    { camera.name !== '' ? (
                                                            camera.name
                                                        ) : (
                                                            '#' + camera.cameraId
                                                        )
                                                    }



                                                    </div>

                                                </div>
                                            )
                                        }
                                }

                               ) 

                            }

                            </div>
                    </div>
                </div>  
        )
    } 
}

module.exports = LocationCameras;

Please read the React documentation : 阅读React文档

Using State Correctly 正确使用状态

There are three things you should know about setState() . 关于setState()您应该了解三件事。

Do Not Modify State Directly 不要直接修改状态

For example, this will not re-render a component: 例如,这不会重新渲染组件:

 // Wrong this.state.comment = 'Hello'; 

Instead, use setState() : 而是使用setState()

 // Correct this.setState({comment: 'Hello'}); 

The only place where you can assign this.state is the constructor. 可以分配this.state的唯一地方是构造函数。

[...] [...]

Use this.setState({updating: false}) insted this.state.updating = false; 使用this.setState({updating: false}) this.state.updating = false; and component will re-render; 并且组件将重新渲染;

To follow @Felix comment, would be better change your function handleClick to 要遵循@Felix注释,最好将您的函数handle更改为

handleClick(){
    this.setState({ updating: !this.state.updating })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM