Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 更新后,我无法重新呈现状态_Reactjs - Fatal编程技术网

Reactjs 更新后,我无法重新呈现状态

Reactjs 更新后,我无法重新呈现状态,reactjs,Reactjs,状态更新后,我无法呈现数据 但一旦我提交,我就会在控制台上获得数据 我正在使用ContextApi 数据 <div> {datas.map(data => { return ( <button key={data.id} onClick={() => handleData(data)} className="btn btn-primary m-4

状态更新后,我无法呈现数据

但一旦我提交,我就会在控制台上获得数据

我正在使用ContextApi

数据

<div>
      {datas.map(data => {
        return (
          <button
            key={data.id}
            onClick={() => handleData(data)}
            className="btn btn-primary m-4"
          >
            {data.title}
          </button>
        );
      })}
      <DetailData />
    </div>

{datas.map(数据=>{
返回(
handleData(数据)}
className=“btn btn主m-4”
>
{data.title}
);
})}
特定数据的详细信息 **其中包含一个输入框,用户将在其中写入注释,注释将显示在上面

**

返回(
{DetailData==null(
) : (
{DetailData.title}
Id:{DetailData.Id}

{DetailData.Description}


注释:{DetailData.comment} )} );
在您的ContextApi中进行此更改,查找添加的此行注释: 通过另一种方式,您需要在提交后更新DetailData

import React, { Component, createContext } from "react";
export const Contx = createContext();

export class ConProvider extends Component {
  state = {
    name: "",
    datas: [
      {
        id: 1,
        title: "Reactjs",
        Description:
          "React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications, as it is optimal for fetching rapidly changing data that needs to be recorded.",
        comment: []
      },
      {
        id: 2,
        title: "Bootstrap",
        Description:
          "Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains CSS- and JavaScript-based design templates for typography, forms, buttons, navigation and other interface components",
        comment: []
      }
    ],
    DetailData: null
  };

  handleData = data => {
    this.setState({
      DetailData: data
    });
  };
  handleInput = e => {
    this.setState({
      [e.target.name]: e.target.value
    });
  };

  handleSubmit = async commentid => {
    const com = this.state.name;
    const updateComment = await this.state.datas.map(c => {
      if (c.id === commentid) {
        return {
          ...c,
          comment: [...c.comment, com]
        };
      } else {
        return c;
      }
    });

    // Added this line
    const dData = await updateComment.filter(c => c.id === commentid);

    this.setState({
      datas: updateComment,
      name: "",
      DetailData: dData[0] // Added this line
    });

  };

  render() {
    return (
      <Contx.Provider
        value={{
          ...this.state,
          handleData: this.handleData,
          handleInput: this.handleInput,
          handleSubmit: this.handleSubmit
        }}
      >
        {this.props.children}
      </Contx.Provider>
    );
  }
}
import React,{Component,createContext}来自“React”;
export const Contx=createContext();
导出类提供程序扩展组件{
状态={
姓名:“,
数据:[
{
id:1,
标题:“Reactjs”,
说明:
“React是一个用于构建用户界面的JavaScript库。它由Facebook和个人开发者和公司社区维护。React可作为开发单页或移动应用程序的基础,因为它最适合获取需要记录的快速变化的数据。”,
评论:[]
},
{
id:2,
标题:“引导”,
说明:
“Bootstrap是一个免费、开源的CSS框架,面向响应性强的移动前端web开发。它包含用于排版、表单、按钮、导航和其他界面组件的基于CSS和JavaScript的设计模板”,
评论:[]
}
],
DetailData:null
};
handleData=数据=>{
这是我的国家({
详细数据:数据
});
};
handleInput=e=>{
这是我的国家({
[e.target.name]:e.target.value
});
};
handleSubmit=async commentid=>{
const com=this.state.name;
const updateComment=等待this.state.datas.map(c=>{
如果(c.id==commentid){
返回{
C
评论:[…c.comment,com]
};
}否则{
返回c;
}
});
//增加了这一行
const dData=await updateComment.filter(c=>c.id==commentid);
这是我的国家({
数据:更新注释,
姓名:“,
DetailData:dData[0]//添加了此行
});
};
render(){
返回(
{this.props.children}
);
}
}
import React, { Component, createContext } from "react";
export const Contx = createContext();

export class ConProvider extends Component {
  state = {
    name: "",
    datas: [
      {
        id: 1,
        title: "Reactjs",
        Description:
          "React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications, as it is optimal for fetching rapidly changing data that needs to be recorded.",
        comment: []
      },
      {
        id: 2,
        title: "Bootstrap",
        Description:
          "Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains CSS- and JavaScript-based design templates for typography, forms, buttons, navigation and other interface components",
        comment: []
      }
    ],
    DetailData: null
  };

  handleData = data => {
    this.setState({
      DetailData: data
    });
  };
  handleInput = e => {
    this.setState({
      [e.target.name]: e.target.value
    });
  };

  handleSubmit = async commentid => {
    const com = this.state.name;
    const updateComment = await this.state.datas.map(c => {
      if (c.id === commentid) {
        return {
          ...c,
          comment: [...c.comment, com]
        };
      } else {
        return c;
      }
    });

    // Added this line
    const dData = await updateComment.filter(c => c.id === commentid);

    this.setState({
      datas: updateComment,
      name: "",
      DetailData: dData[0] // Added this line
    });

  };

  render() {
    return (
      <Contx.Provider
        value={{
          ...this.state,
          handleData: this.handleData,
          handleInput: this.handleInput,
          handleSubmit: this.handleSubmit
        }}
      >
        {this.props.children}
      </Contx.Provider>
    );
  }
}