1

I'm using functional components and React-Redux to manage state. Simply speaking, I hope that even if the state changes, rendering will not happen. First of all, the code is as follows. It is a simple code for explanation.

const NOUPDATE = ({ uploadAnswer, noState }) => (
  <>
    <div className="content">
      <div className="container">
        <p className="directions" />
        <ul className="qbox">{noState ? (<p>true</p>) : (<p>false</p>)}</ul>
        <button onClick={uploadAnswer}>upload</button>
      </div>
    </div>
  </>
);
const mapStateToProps = state => ({
  noState: state.problemInfoReducer.checkMode,
});
const mapDispatchToProps = dispatch => ({
  uploadAnswer: value => dispatch(uploadAnswer(value)),
});

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(NOUPDATE);

uploadAnswer reverses noState each time it is dispatched. So if I dispatch uploadAnswer every time, noState is updated, and the screen is re-rendered as a whole. I don't want the screen to be re-rendered even if noState is updated.

2 Answers 2

1

Here is a solution that allows you to keep the qbox boolean toggle without rendering the parent component. Essesntially, you use Redux to your advantage by creating a child component that's mapped to the noState property rather than having that mapping in the parent component.

NOUPDATE.jsx

import Qbox from "./Qbox";

const NOUPDATE = ({ uploadAnswer }) => (
  <>
    <div className="content">
      <div className="container">
        <p className="directions" />
        <Qbox />
        <button onClick={uploadAnswer}>upload</button>
      </div>
    </div>
  </>
);

const mapDispatchToProps = dispatch => ({
  uploadAnswer: value => dispatch(uploadAnswer(value)),
});

export default connect(
  null,
  mapDispatchToProps,
)(NOUPDATE);

Qbox.jsx

const Qbox = ({noState}) => <ul className="qbox">{noState ? (<p>true</p>) : (<p>false</p>)}</ul>;

const mapStateToProps = state => ({
  noState: state.problemInfoReducer.checkMode,
});

export default connect(
  mapStateToProps
)(Qbox);

Sign up to request clarification or add additional context in comments.

2 Comments

IT WORKS NICK! THANK YOU!
Hooray, glad I could help!
0

You can use React.memo to memoize your component. The second argument passed to React.memo is a function that takes oldProps and newProps. Return false when the component should be updated.

Below, I return false only when the uploadAnswer doesn't match between oldProps and newProps, so that's the only time the component will re-render.

const NOUPDATE = React.memo(
  ({ uploadAnswer, noState }) => (
    <>
      <div className="content">
        <div className="container">
          <p className="directions" />
          <ul className="qbox">{noState ? <p>true</p> : <p>false</p>}</ul>
          <button onClick={uploadAnswer}>upload</button>
        </div>
      </div>
    </>
  ),
  (oldProps, newProps) => {
    if (oldProps.uploadAnswer !== newProps.uploadAnswer) {
      return false;
    }

    return true;
  }
);

5 Comments

thank you for your help! but I got additional question. As you can see, my 'qbox' class's p tag is not working. I only want to change p tag, true or false. Is it possible?
Hmm, I don't quite follow; what do you mean it's not working?
Well, I'd like to ask you in advance that I think it might be a pretty strange question. In my qbox, the contents of the p tag are either true or false according to noState. It's like a toggle button. But, of course, it doesn't work like a toggle button because it doesn't re-render.
Yes, that's going to be problematic! You can't have it both ways. What you can do is create a separate element that only takes noState and map that to props using Redux. You can then remove the noState prop dependency within this component.
I'm adding a new answer that should hopefully do what you're looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.