score:1
The class field map
is initialized only once, at first component render. It does not really matter that you change the state, that variable holds the very same data and does not change in time.
One of the solutions would be moving that variable into render:
render() {
const map = {
"A": (<Section primaryNode={this.primaryNode()} bottom={this.bottom()} header={"A"} />),
"B": (<Section primaryNode={this.primaryNode()} header={"B"} />),
"C": (<Section primaryNode={this.primaryNode()} header={"C"} />)
};
return (<div>{this.data.map(d => this.map[d])}</div>)
}
or you could transform map
field into a method:
map = () => ({
"A": (<Section primaryNode={this.primaryNode()} bottom={this.bottom()} header={"A"} />),
"B": (<Section primaryNode={this.primaryNode()} header={"B"} />),
"C": (<Section primaryNode={this.primaryNode()} header={"C"} />)
});
render() {
return (<div>{this.data.map(d => this.map()[d])}</div>)
}
So the data returned by map
is always up-to-date.
score:0
Try moving the map
inside the render()
method.
render()
is fired when the state changes so it's the place You're looking for
score:1
I think you're making this more confusing than it needs to be by holding all these values as class properties. The reason that they are not updating is because this.map
is only defined once. You don't need map at all, just render your nodes directly inside render
function which gets called on state changes:
render() {
return (
<div>
<Section primaryNode={this.primaryNode()} bottom={this.bottom()} header={"A"} />
<Section primaryNode={this.primaryNode()} header={"B"} />
<Section primaryNode={this.primaryNode()} header={"C"} />
</div>
)
}
And if you really want it to be a map, then define it inside render
:
render() {
const map = {
"A": <Section primaryNode={this.primaryNode()} bottom={this.bottom()} header={"A"} />,
"B": <Section primaryNode={this.primaryNode()} header={"B"} />,
"C": <Section primaryNode={this.primaryNode()} header={"C"} />
};
return <div>{this.data.map(d => map[d])}</div>
}
score:1
Not entirely sure what you're wanting to do, but it sounds like you want your A/B/C values to be updated with state-change. The problem is that you are defining the primaryNode in your map-object before render()
, rather than inside of render()
(which always re-renders when state changes. Try this:
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
const Section = item => (
<>
<br/>
<section>
{item.header}
<br />
{item.primaryNode}
<br />
{item.bottom}
</section>
</>
);
class App extends React.PureComponent {
constructor(props) {
super(props);
}
state = {
current: 'B'
}
data = ["A", "B", "C"];
show = [5, 6, 3, 5, 7, 8, 77, 8, 90];
bottom = () => (
<select name={"fruit"} value={this.state.current} onChange={this.handleChange}>
<option value="A">Apple</option>
<option value="B">Banana</option>
<option value="C">Cranberry</option>
</select>
);
handleChange = (event) => (
this.setState({ current: event.target.value })
);
primaryNode = () => (
<div className="primary-node">
{this.show} ---- {this.state.current}
</div>
);
render() {
return (
<>
State Value - {this.state.current}
<br/>
<br/>
<br/>
{this.data.map(el => (
<Section
bottom={el === "A" && this.bottom()}
header={el}
primaryNode={this.primaryNode()}
/>
))}
</>
);
}
}
render(<App />, document.getElementById('root'));
Source: stackoverflow.com
Related Query
- Map of React Components not re rendering on State Change
- React State change is not Rendering
- react stateless child components do not update on state change in parent component
- REACT + REDUX: on redux state change mapStateToProps updating state but view is not rendering as per new state
- React not rendering updated UI on state change
- React / Redux Components not re-rendering on state change
- React JS - Change parent state on child click, map not iterable
- React components not rendering from global state using context API
- React Functional Components change state of parent from child without rendering all children
- React components not updating with state change
- React component not rendering after state change
- React + Mapbox GL: State of Array of Map Components not updating on Map Event
- Components do not re-render on state change react
- React component not rendering after state change from callback
- react router not rendering components on route change
- React Child Component Not Updating After Parent State Change
- React component not re-rendering on state change
- React Hooks - useEffect fires even though the state did not change
- React shouldComponentUpdate() is called even when props or state for that component did not change
- React leaflet center attribute does not change when the center state changes
- Props not updating when redux state change in React Hooks
- Components not re-rendering on route change - React HashRouter
- react is not updating functional component state on input change
- React Router v4 not rendering components
- Google Map is Not Rendering in React App
- react-router-dom not rendering components on route change
- react component state change after ajax call but does not rerender component
- Conditional Rendering in React won't work, state not working properly?
- Nested map is not rendering the Redux State Correctly
- React Child with useEffect() not updating on Parent State Change
More Query from same tag
- Axios AJAX call in React returning only initial state
- React redux slider
- Check if props are undefined when loading component
- How can I style the arrow of react-multi-select-component?
- Mock a custom service with jest and enzyme for a react component
- Store GraphQL errors as String
- Render component object inside array dynamically based on props
- I have a weird issue with react-modal
- React Mocha rendering, DOMException Wrong Document
- How can I change svg icon colors in React?
- Creating an Alias for an NPM Package in Gatsby Using the Root Import Plugin
- How to protect breaking app when state is initialized by props of undefined?
- How to implement AddAdiditions in React Sematic UI using Hooks?
- Access parent context when using this.props.children in React
- unique keys are unique, are assigned to key prop, but still getting error... but only in one component
- want to increase previous and next page pagination icon size in material react table
- how to check if the props in a jsx tag exists?
- React importing and rendering other component not working without tooling
- Getting Error: console is not defined in redux
- How can i play audio file sent from flask send_file?
- Recommended way to share components between nextjs zones?
- How to config jest jsdom environment?
- React Router v4 get current location
- How do I mock a custom property of the window object in Jest?
- How to get the headers of a reponse in a fetch request?
- axios GET request function export/ import in react
- How to load the API data to react component once the promise is resolved?
- React - Create nested components with loops
- How to get the update and delete endpoints to function properly in my React App?
- SQL search and JavaScript .map is not a function