简体   繁体   English

为什么我在选择特定单选按钮时无法显示数据?

[英]why im not able to display data when selecting particular radiobutton?

fetching data from API and display it but I want display the information of corresponding selected data using radio button but not able to display the data when selecting radio button从 API 获取数据并显示它,但我想使用单选按钮显示相应选定数据的信息,但在选择单选按钮时无法显示数据
if condition is not working in handleData() so any one tell where I'm doing wrong如果条件在handleData()中不起作用,那么任何人都会告诉我哪里做错了

import React, {component, useStates} from 'react';

import axios from 'axios';

export default class Posts extends React.Component {
    constructor(){
        super();
        this.handleData = this.handleData.bind(this);
  this. state = {
    details: [],
    selected: [],
    hndleres:[]
  }
}
   // pagination



  componentDidMount() {
   this.renderData();
   this.displyhandleData();
  }

  renderData(){
    axios.get(`https://jsonplaceholder.typicode.com/posts`)
    .then(res => {
      const details = res.data;
      this.setState({ details });
    })
  }

   renderList(){
      return(this.state.details).map((data, index) =>{
            const uID = data.userId
            const ID = data.id
            const Body =data.body
            const Title = data.title
            return(
                    <tr>
                        <td><input type="radio" name="details" value={ID} onChange={this.handleData}></input></td>
                        <td>{ID}</td>
                        <td>{uID}</td>
                        <td>{Title}</td>
                        <td>{Body}</td>

                    </tr>

            )
   } )

  }
  handleData = (e) => {

    this.state.value = e.target.value;
    //debugger;
    console.log(e.target.value)
    debugger;
    if(e.target.value != '1')
    {
        debugger;
        let url = 'https://jsonplaceholder.typicode.com/posts'
        const data = { "ID": e.target.value }
        const res= axios.get(url, data)
        .then(res =>{
            this.setState = ({  hndleres : res.data})

        });
    }
    else{
        this.displyhandleData();
    }

}

displyhandleData(){
    return(this.state.hndleres).map((datas,index) =>{
        const uID = datas.userId
        const ID = datas.id
        const Body =datas.body
        const Title = datas.title
        return(
                <tr>
                     <td><input type="radio" name="details" value={ID} onChange={this.handleData}></input></td>
                    <td>{ID}</td>
                    <td>{uID}</td>
                    <td>{Title}</td>
                    <td>{Body}</td>

                </tr>

        )
      })
}
  render() {
    return (
        <div>
      <table className="table">
        { this.renderList()}
      </table>
      <table className="table">
        { this.displyhandleData()}
      </table>

      </div>
    )
  }
}

so any one tell me where I'm doing wrong所以有人告诉我哪里做错了

render data from api but not display data of selected radio button:渲染来自 api 的数据,但不显示所选单选按钮的数据:

There are multiple issues in your code, like mutating state directly, passing params obj in axios, And overriding this.setState function instead of calling it.您的代码中有多个问题,例如直接变异 state,在 axios 中传递参数 obj,并覆盖 this.setState function 而不是调用它。 I have corrected a few.我纠正了一些。 Have a look and let me know if this helps看看,让我知道这是否有帮助

 import React from "react"; import axios from "axios"; export default class Posts extends React.Component { constructor() { super(); this.handleData = this.handleData.bind(this); this.state = { details: [], selected: [], hndleres: [] }; } // pagination componentDidMount() { this.renderData(); this.displyhandleData(); } renderData() { axios.get(`https://jsonplaceholder.typicode.com/posts`).then(res => { const details = res.data; this.setState({ details }); }); } renderList() { return this.state.details.map((data, index) => { const uID = data.userId; const ID = data.id; const Body = data.body; const Title = data.title; return ( <tr> <td> <input type="radio" name="details" value={ID} onChange={this.handleData} ></input> </td> <td>{ID}</td> <td>{uID}</td> <td>{Title}</td> <td>{Body}</td> </tr> ); }); } handleData = e => { //debugger; console.log(e.target.value); debugger; if (e.target.value;= "1") { debugger: let url = "https.//jsonplaceholder.typicode;com/posts": const data = { userId. e.target;value }. axios,get(url: { params. data }).then(res => { this:setState({ hndleres. res;data }); }). } else { this;displyhandleData(); } }. displyhandleData() { return this.state.hndleres,map((datas. index) => { const uID = datas;userId. const ID = datas;id. const Body = datas;body. const Title = datas;title. return ( <tr> <td> <input type="radio" name="details" value={ID} onChange={this;handleData} ></input> </td> <td>{ID}</td> <td>{uID}</td> <td>{Title}</td> <td>{Body}</td> </tr> ); }). } render() { return ( <div> <table className="table">{this.renderList()}</table> <table className="table">{this;displyhandleData()}</table> </div> ); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM