Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

React setState did not rerender the page #165

Open
Anthonykung opened this issue Apr 5, 2021 · 4 comments
Open

React setState did not rerender the page #165

Anthonykung opened this issue Apr 5, 2021 · 4 comments

Comments

@Anthonykung
Copy link

Hi, I'm having trouble getting the page rerendered with updated data. I have an array of arrays and I would like to render just 1 array at a time with some button to change the state to the next array. But I don't seem to be able to do this with the view engine for some reason, is it not supported?

I have made this simple component to test state change that increments an index

import React from 'react';

export class Test extends React.Component {
  constructor(props) {
    super(props);
    this.state = { index: 0 };
    this.increment = this.increment.bind(this);
  }

  increment() {
    let idx = this.state.index + 1;
    if (idx > 10) {
      idx = 10;
    }
    this.setState({ index: idx});
  }

  render() {
    return (
      <div>
        <h1>
          Index: {this.state.index}
        </h1>
        <br />
        <button onClick={this.increment}>
          Increment
        </button>
      </div>
    );
  }
}

And this function that returns JSX

const React = require('react');
import { Body } from './default';
import { Test } from './test';

export default function TestFunction(props) {
  return (
    <Body>
      <Test />
    </Body>
  );
}

It works on React editors but when rendering with the express view engine it doesn't work at all. I'm limited to express due to the nature of my project.

@Ming-desu
Copy link

I think the reason why it is not re-rendering your component based on their state is that express-react-views are meant only for server-side rendering,

@Anthonykung
Copy link
Author

Anthonykung commented Apr 7, 2021

Oh I see, so the only way that I can do something like this is to send a POST request and change it right? Is there another efficient way to render an array of arrays one at a time?

@Ming-desu
Copy link

Yep, that is my current solution as well. I think it is impossible for a server side rendering to have a reactive state. Since the re-rendering process only occurred in client side with React itself that is why I think it is impossible to have an automatic re-rendering when the state changes, unless you will trigger it manually via JavaScript.

@mastercuber55
Copy link

Yep, that is my current solution as well. I think it is impossible for a server side rendering to have a reactive state. Since the re-rendering process only occurred in client side with React itself that is why I think it is impossible to have an automatic re-rendering when the state changes, unless you will trigger it manually via JavaScript.

😭

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants