简体   繁体   English

反应-变量未正确显示在页面中

[英]React - variable not showing in page correctly

I have a variable attackHero whose initial value is Ironman. 我有一个初始值为Ironman的变量attackHero Its showing correctly on the page. 它正确显示在页面上。 On button click I am changing it to Hulk via Redux dispatch. 在按钮上单击,我通过Redux分派将其更改为Hulk。 But the change is not reflecting in the page. 但是更改未反映在页面中。 Am I missing something here? 我在这里想念什么吗?

Please see the code below. 请参见下面的代码。

import React, { Component } from 'react';
import { createStore } from 'redux';

class ReduxDemo extends Component {

    constructor(props) {
        super(props);
        this.initNukeAttack = this.initNukeAttack.bind(this);

        this.store = null;
    }

    initNukeAttack() {
        console.log("I am inside initNukeAttack");
        this.store.dispatch({type:"GREEN-ATTACK", hero: "Hulk"});
    }
    render() {
        let attackHero = "attack hero";

        // step 2 -> reducer => require state and action
        const reducer = function(state, action) {
            if(action.type === "ATTACK") {
                return action.hero;
            }
            if(action.type === "GREEN-ATTACK") {
                return action.hero;
            }
            return {p:"peace"};
        }

        //Step 1
        this.store = createStore(reducer, "PEACE");

        //step 3 -> subscribe
        this.store.subscribe(() => {
            //console.log("Store is now", store.getState());
            //console.log(store.getState());

            attackHero = this.store.getState();
        })

        //step 4 -> dispatch action
        this.store.dispatch({type:"ATTACK", hero: "Ironman"});
        //this.store.dispatch({type:"GREEN-ATTACK", hero: "Hulk"});
        return(
            <div>
                <div>{attackHero}</div>
                <button onClick={this.initNukeAttack}>Initiate Green Attack</button>
            </div>
        );
    }
}

export default ReduxDemo;

The rendered screen look like this. 渲染的屏幕如下所示。

在此处输入图片说明

Hi first i would recommend to properly follow react with redux with middleware for action cretors. 嗨,首先,我建议针对动作创作者,适当地跟上redux和中间件的反应。 There are abundant resources available. 有大量可用资源。 Anyways you were dispatching an action in render which is wrong. 无论如何,您都是在渲染中调度一个操作,这是错误的。 Second, for updating the the variable you need to setState to re-render your component. 其次,要更新变量,您需要setState重新渲染组件。 Here is your working code : 这是您的工作代码:

class ReduxDemo extends Component {
  constructor(props) {
    super(props);
    this.initNukeAttack = this.initNukeAttack.bind(this);

    this.store = null;
    this.state = {
      attackHero: "IronMan"
    };
  }

  initNukeAttack() {
    console.log("I am inside initNukeAttack");
    this.store.dispatch({ type: "GREEN-ATTACK", hero: "Hulk" });
  }
  render() {
    // step 2 -> reducer => require state and action
    const reducer = function(state = "ironman", action) {
      if (action.type === "ATTACK") {
        return action.hero;
      }
      if (action.type === "GREEN-ATTACK") {
        return action.hero;
      }
      return state;
    };

    //Step 1
    this.store = createStore(reducer, "PEACE");

    //step 3 -> subscribe
    this.store.subscribe(() => {
      //console.log("Store is now", store.getState())

      const attackHero = this.store.getState();
      this.setState({
        attackHero
      })
    });

    //step 4 -> dispatch action
    //this.store.dispatch({ type: "ATTACK", hero: "Ironman" });
    // this.store.dispatch({type:"GREEN-ATTACK", hero: "Hulk"});
    return (
      <div>
        <div>{this.state.attackHero}</div>
        <button onClick={this.initNukeAttack}>Initiate Green Attack</button>
      </div>
    );
  }
}

But the change is not reflecting in the page 但是更改未反映在页面中

this is because React is not re-rendering the page. 这是因为React不会重新渲染页面。

To re-render you need to use state and set the state variable via setState method. 要重新渲染,您需要使用state并通过setState方法设置状态变量。

or 要么

you need to force render the page like this.forceUpdate(); 您需要像这样强制渲​​染页面this.forceUpdate(); after you set the state variable 设置状态变量后

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

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