Gatsby-Redux:组件不会在状态更改时重新呈现

时间:2019-10-27 10:17:16

标签: javascript reactjs redux state gatsby

在我的Redux项目中实现Gatsby时,我发现当组件的状态改变时,我的组件不会重新渲染(是的,我将状态视为不可变的)。

因此,我使用Redux的Conuter example,并将其代码复制到我的Gatsby项目中。 并且问题再次发生,状态改变时组件无法重新渲染(使用Redux的示例之一!)。

仅通过使用this.forceUpdate(),我才能使组件在状态更改时重新呈现,而当我删除此行时,它停止在状态更改时重新呈现。

这是我创建的Gatsby页面以及我复制到的Redux代码:

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

class Counter extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    console.log("render - start",JSON.stringify(store.getState(),null,2))
    const { value, onIncrement, onDecrement } = this.props
    return (
      <p>
        Clicked: {value} times
        {' '}
        <button onClick={onIncrement}>
          +
        </button>
        {' '}
        <button onClick={onDecrement}>
          -
        </button>
      </p>
    )
  }
}

const counter = (state = 0, action) => {
  console.log("reducer - start: ",state, JSON.stringify(action,null,2))
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}

const store = createStore(counter)

class TestPage extends React.Component {
  render(){
    return (
      <Counter
        value={store.getState()}
        onIncrement={() => {
          store.dispatch({ type: 'INCREMENT' })
          // this.forceUpdate()
        }}
        onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
      />)
  }
}

export default TestPage

1 个答案:

答案 0 :(得分:1)

您正确存储了.subsribe()吗?

我让它像这样工作。

index.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import { store } from "./store";
import Counter from "./Counter";

class App extends Component {
  render() {
    return (
      <Counter
        value={store.getState()}
        onIncrement={() => {
          store.dispatch({ type: "INCREMENT" });
        }}
        onDecrement={() => store.dispatch({ type: "DECREMENT" })}
      />
    );
  }
}

const rootElement = document.getElementById("root");
const render = () => ReactDOM.render(<App />, rootElement);

render();
store.subscribe(render);

store.js

import { createStore } from "redux";

const counter = (state = 0, action) => {
  switch (action.type) {
    case "INCREMENT":
      return state + 1;
    case "DECREMENT":
      return state - 1;
    default:
      return state;
  }
};


export const   store = createStore(counter);

Counter.js

import React, { Component } from 'react'

class Counter extends Component {

  render() {

    const { value, onIncrement, onDecrement } = this.props
    return (
      <p>
        Value: {value} 
        {' '}
        <button onClick={onIncrement}>
          +
        </button>
        {' '}
        <button onClick={onDecrement}>
          -
        </button>
      </p>
    )
  }
}

export default Counter;

示例代码和框: https://codesandbox.io/s/redux-counter-2oftu

通过在像这样的react中使用redux的方式没有多大意义,我们通常使用react-redux包。在学习了redux基础知识之后,您绝对应该使用react-redux软件包。

https://react-redux.js.org/