Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

state update not being reflected on mapped elements

In the following code, the tags are being perfectly displayed after set state but not being reflected on DOM change.

In the following code, the tags are being perfectly displayed after set state but not being reflected on DOM change.

import React from 'react';
import ReactDOM from 'react-dom/client';
import 'bootstrap/dist/css/bootstrap.css';
import PasteImage from './components/builder/q-img-paste';
import PreTag from './components/builder/q-pre-tag'
import QuestionDisplay from './components/builder/q-display';

class App extends React.Component{
  
  state = {
    questions : [],
    pretag : ""
  }  

  addImage (q){
    this.setState({
      questions : [q, ...this.state.questions]
    })
  }

  updatePretag(q){
    this.setState({pretag: q})
    // Here, it is working perfectly
    // Here, it is working perfectly
    // Here, it is working perfectly        
    console.log(this.state.pretag) 
    // Here, it is working perfectly
    // Here, it is working perfectly
    // Here, it is working perfectly
  }

  render (){
    return (
      <React.Fragment>
        <PreTag updatePretag={(p) => this.updatePretag(p)}/>
        <PasteImage addImage={(q) => this.addImage(q)} preTag={this.state.preTag} />
        <div className="row" id="img-display">
          {
             this.state.questions.map(q => 
              {
                // This is not shown here
                // This is not shown here
                // This is not shown here
                // This is not shown here
                console.log(this.state.pretag); 
                // This is not shown here
                // This is not shown here
                // This is not shown here
                // This is not shown here
                return <QuestionDisplay key={q["id"]} url={q["url"]} 
                id={q["id"]} tags={q["tags"]} />
              })
          }
        </div>
      </React.Fragment>
    )
  }
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have a state that contains an array and a string. you are maping the array questions in JSX so it will only reflect changes in question state and it will not render or reflect any change in question string. you are accessing question by this.state.question.map

and there is no state preTag you are not maping it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading