Reactjs:如何使用Reactjs在弹出按钮单击时正确地从数据库中获取每个用户记录

时间:2019-02-13 08:42:35

标签: reactjs

下面的代码显示了有关用户列表按钮单击的每个用户信息。 现在,我要从用户列表按钮单击上的数据库中获取每个用户记录。

open()函数中,我已经实现了以下代码

open = (id,name) => {
    alert(id);
    alert(name);

    //start axios api call

    const user_data = {
        uid: 'id',
        uname: 'name'
    };

    this.setState({ loading_image: true }, () => {
    axios.post("http://localhost/data.php", { user_data })  
           .then(response => {
               this.setState({
                   chatData1: response.data[0].id,
                   chatData: response.data,
                   loading_image: false
               });
                console.log(this.state.chatData);
                alert(this.state.chatData1);
           })
           .catch(error => {
             console.log(error);
           });
     });
}

Class OpenedUser()类中,我已在构造函数中初始化了以下代码

 chatData: [] 

在render方法中已经实现了代码

<b> Load Message from Database for each user ({this.state.chatData1})</b>
<div>
 {this.state.chatData.map((pere, i) => (<li key={i}>{pere.lastname} - {pere.id}-----  {pere.username}</li>))}
</div>

这是我的问题:

我的问题是Axios Api正在获取结果,但在render方法中看不到任何结果。  但我可以按照以下代码在控制台中看到它 数组(1) 0:{id:“ 1”,名字:“ faco”,姓氏:“ facoyo”} 长度:1

以下是json api响应的示例。

[{"id":"1","firstname":"faco","lastname":"facoyo"}]

这是完整的代码

import React, { Component, Fragment } from "react";
import { render } from "react-dom";
import { Link } from 'react-router-dom';
import axios from 'axios';



class User extends React.Component {

  open = () => this.props.open(this.props.data.id, this.props.data.name);


  render() {

    return (
      <React.Fragment>
        <div key={this.props.data.id}>
          <button onClick={() => this.open(this.props.data.id,this.props.data.name)}>{this.props.data.name}</button>
        </div>
      </React.Fragment>
    );
  }
}


class OpenedUser extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
 chatData: [],

      hidden: false,
    };
  }





componentDidMount(){

} // close component didmount

  toggleHidden = () =>
    this.setState(prevState => ({ hidden: !prevState.hidden }));

  close = () => this.props.close(this.props.data.id);

  render() {

    return (
      <div key={this.props.data.id} style={{ display: "inline-block" }}>
        <div  className="msg_head">

          <button onClick={this.close}>close</button>
          <div>user {this.props.data.id}</div>
          <div>name {this.props.data.name}</div>
          {this.state.hidden ? null : (
            <div className="msg_wrap">
              <div className="msg_body">Message will appear here</div>



<b> Load Message from Database for each user ({this.state.chatData1}) </b>
<div>
 {this.state.chatData.map((pere, i) => (
              <li key={i}>
                {pere.lastname} - {pere.id}-----  {pere.username} 
              </li>
            ))}



          </div>



            </div>
          )}
        </div>
      </div>
    );
  }
}
class App extends React.Component {
  constructor() {
    super();

    this.state = {
      shown: true,
      activeIds: [],
      data: [
        { id: 1, name: "user 1" },
        { id: 2, name: "user 2" },
        { id: 3, name: "user 3" },
        { id: 4, name: "user 4" },
        { id: 5, name: "user 5" }
      ],
    };

  }






toggle() {
        this.setState({
            shown: !this.state.shown
        });
    }


  open = (id,name) => {

alert(id);
alert(name);

//start axios api call

const user_data = {
    uid: 'id',
    uname: 'name'
    };

this.setState({ loading_image: true }, () => {
axios.post("http://localhost/apidb_react/search_data.php", { user_data })

           .then(response => {
             this.setState({

         chatData1: response.data[0].id,
         chatData: response.data,
               loading_image: false
             });

console.log(this.state.chatData);
alert(this.state.chatData1);

           })
           .catch(error => {
             console.log(error);
           });

     });



// end axios api call


   this.setState((prevState) => ({
      activeIds: prevState.activeIds.find((user) => user === id)
        ? prevState.activeIds
        : [...prevState.activeIds, id]
    }));

  }

  close = id => {
    this.setState((prevState) => ({
      activeIds: prevState.activeIds.filter((user) => user !== id),
    }));
  };

  renderUser = (id) => {
    const user = this.state.data.find((user) => user.id === id);
    if (!user) {
      return null;
    }
    return (
      <OpenedUser key={user.id} data={user} close={this.close}/>
    )
  }




  renderActiveUser = () => {
    return (
      <div style={{ position: "fixed", bottom: 0, right: 0  }}>
        {this.state.activeIds.map((id) => this.renderUser(id)) }
      </div>
    );
  };


  render() {

    return (

      <div>



        {this.state.data.map(person => (
          <User key={person.id} data={person} open={this.open} />
        ))}
        {this.state.activeIds.length !== 0 && this.renderActiveUser()}
      </div>
    );
  }
}

2 个答案:

答案 0 :(得分:0)

我在您的代码中看到了一些遗漏点,即您使用的是li而没有ul,这是一种无效标记,然后您对.username进行了映射,该映射是根据响应未定义的字段,也可能会引发错误。 >

答案 1 :(得分:0)

问题是,您正在App组件中发出请求并存储在状态中,但是您试图在子组件中访问该状态,因此它将永远不会真正读取数据。

要解决此问题,您需要通过道具传递聊天数据

      <OpenedUser
        chatData={this.state.chatData}
        key={user.id}
        data={user}
        close={this.close}
      />

注意:在我的可运行示例中,我已将您的api端点替换为模拟api承诺。

const mockApi = () => {
  return new Promise((resolve, reject) => {
    const json = [{ id: "1", firstname: "faco", lastname: "facoyo" }];
    resolve(json);
  });
};

class User extends React.Component {
  open = () => this.props.open(this.props.data.id, this.props.data.name);
  render() {
    return (
      <React.Fragment>
        <div key={this.props.data.id}>
          <button
            onClick={() => this.open(this.props.data.id, this.props.data.name)}
          >
            {this.props.data.name}
          </button>
        </div>
      </React.Fragment>
    );
  }
}

class OpenedUser extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      hidden: false
    };
  }

  componentDidMount() {} // close component didmount

  toggleHidden = () =>
    this.setState(prevState => ({ hidden: !prevState.hidden }));

  close = () => this.props.close(this.props.data.id);

  render() {
    return (
      <div key={this.props.data.id} style={{ display: "inline-block" }}>
        <div className="msg_head">
          <button onClick={this.close}>close</button>
          <div>user {this.props.data.id}</div>
          <div>name {this.props.data.name}</div>
          {this.state.hidden ? null : (
            <div className="msg_wrap">
              <div className="msg_body">Message will appear here</div>

              <b>
                {" "}
                Load Message from Database for each user ({this.state.chatData1}
                ){" "}
              </b>
              <ul>
                {this.props.chatData.map((pere, i) => (
                  <li key={i}>
                    {pere.lastname} - {pere.id}----- {pere.username}
                  </li>
                ))}
              </ul>
            </div>
          )}
        </div>
      </div>
    );
  }
}
class App extends React.Component {
  constructor() {
    super();

    this.state = {
      shown: true,
      chatData: [],
      activeIds: [],
      data: [
        { id: 1, name: "user 1" },
        { id: 2, name: "user 2" },
        { id: 3, name: "user 3" },
        { id: 4, name: "user 4" },
        { id: 5, name: "user 5" }
      ]
    };
  }

  toggle() {
    this.setState({
      shown: !this.state.shown
    });
  }

  open = (id, name) => {
    alert(id);
    alert(name);

    //start axios api call

    const user_data = {
      uid: "id",
      uname: "name"
    };

    // this.setState({ loading_image: true }, () => {
    //   axios
    //     .post("http://localhost/apidb_react/search_data.php", { user_data })

    //     .then(response => {
    //       this.setState({
    //         chatData1: response.data[0].id,
    //         chatData: response.data,
    //         loading_image: false
    //       });

    //       console.log(this.state.chatData);
    //       alert(this.state.chatData1);
    //     })
    //     .catch(error => {
    //       console.log(error);
    //     });
    // });

    this.setState({ loading_image: true }, () => {
      mockApi().then(data => {
        this.setState({
          chatData1: data[0].id,
          chatData: data,
          loading_image: false
        });
      });
    });

    // end axios api call

    this.setState(prevState => ({
      activeIds: prevState.activeIds.find(user => user === id)
        ? prevState.activeIds
        : [...prevState.activeIds, id]
    }));
  };

  close = id => {
    this.setState(prevState => ({
      activeIds: prevState.activeIds.filter(user => user !== id)
    }));
  };

  renderUser = id => {
    const user = this.state.data.find(user => user.id === id);
    if (!user) {
      return null;
    }
    return (
      <OpenedUser
        chatData={this.state.chatData}
        key={user.id}
        data={user}
        close={this.close}
      />
    );
  };

  renderActiveUser = () => {
    return (
      <div style={{ position: "fixed", bottom: 0, right: 0 }}>
        {this.state.activeIds.map(id => this.renderUser(id))}
      </div>
    );
  };

  render() {
    return (
      <div>
        {this.state.data.map(person => (
          <User key={person.id} data={person} open={this.open} />
        ))}
        {this.state.activeIds.length !== 0 && this.renderActiveUser()}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>