score: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);
score: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;
}
);
Source: stackoverflow.com
Related Query
- How to prevent Re-render in react when the state is changed using Functional Component
- How to get the changed state after an async action, using React functional hooks
- How to pass the match when using render in Route component from react router (v4)
- How to stop re render child component when any state changed in react js?
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Updating state of a functional component inside the render method
- how do I render a page in react using functional components when onSubmit?
- How to prevent React from re-rendering the whole component when using setInterval
- How to change the state of the particular component when it is clicked, using useState in react js?
- How do I access the 'currentImageIdIndex' when using the stack scroll tool in Cornerstone.js in a React functional component?
- How to setup a generic interface for the state when using React useReducer with typescript
- How to render component conditionally when leaf value of global state changes using react hooks
- When using React / Redux, how is dispatching an action that merely changes the Redux state vs actually doing some task?
- How to disable a button when the state or the input value gets changed in React JS?
- How to update the state in functional component using React Hooks
- How to cause a component to re render in react when using a hook which holds some state
- How to disable/enable button when the iput field is empty or filled using state in react js?
- How to type the initial state for react context when using useState as value
- How to set the state of a react functional component using jest
- How to hide a button when I click the button in react using functional components
- React - How to pass props down for the .map function when using functional components
- When we render react Components from an iterable using Array.prototype.map() how is the index generated, and how does react uses it?
- How do I use react hooks to tidy up a functional component when it unmounts (using values from state in the tidy up)
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- How to select a single list element when using map in React to alter the state value
- How to re-render a page by updating the state In React using functional component.?
- How to prevent child component from re-rendering when using React hooks and memo?
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- How to initialize the react functional component state from props
- CDN links for React packages and how to import it when using react using the scripts from CDN
- How do I store the state of radio groups in React using react-hook-form
More Query from same tag
- Firebase and React hosting - not loading App.js
- What is the most efficient way to toggle between two components React JS?
- Div is not at the right place in production (using chakraUi)
- How to avoid reconnections with socket.io-client and React app?
- File data shows empty object when sent to server but contains data before sending via axios api
- What is the proper flow to merge the previous state with the new state on a useState hook?
- URL redirect from event handlers in React v4
- How to implement responsive layout in Ant Design form?
- Serve large static page from React
- concatenation setState with dynamic key in react using es6
- Handle Google OAuth with JWT (react + nodejs)
- useEffect infinite loop occurs only while testing, not otherwise - despite using useReducer
- How to bind a function's "this" to a class (React Component)
- How do I access the name of the city nested in this api response?
- CSS Animation, onAnimationEnd Running only once
- Linking a Bootstrap Navbar correctly with nextjs
- how to change the status code in Codeigniter 4 To push pages in ReactJS
- Filter array of objects matching values from second array
- React-Redux How to improve code in Reducers file
- Java Class Boolean not accepting null
- NextJs routing to the same page with different query
- Unable to start project after create-react-app: Error: Cannot find module 'C:\Users\pcname\Documents\react-scripts\bin\react-scripts.js'
- How to debug this search filter in react
- Modular CSS and App-wide Styles
- Using react-router-dom in my project,how to make menu is above on main
- How to execute React D3 component on localhost
- How do I test a jest console.log
- Render image with json data | ReactJs
- Cart not adding correct item in React.js
- ReactJS call class method in Button after map()