首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >状态更新没有反映在映射的元素上

状态更新没有反映在映射的元素上
EN

Stack Overflow用户
提问于 2022-09-05 07:17:15
回答 1查看 23关注 0票数 1

在下面的代码中,tags是在set状态之后完美地显示出来的,但没有反映在DOM更改上。

在下面的代码中,tags是在set状态之后完美地显示出来的,但没有反映在DOM更改上。

代码语言:javascript
复制
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>
);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-05 07:24:17

您有一个包含数组和字符串的状态。您将在JSX中映射数组问题,因此它只会反映问题状态的更改,而不会呈现或反映问题字符串中的任何更改。您正在通过this.state.question.map访问问题

并且没有状态preTag,你没有映射它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73605781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档