0

I have a json data that I fetch from api. Let's say the table is (The below table is just an example)

  Name | Age
   A      20
   B.     25

Now I am fetching the name and age content from an api and displaying it as a table format( I am using React js) Now, I want to make the content of name such as A and B clickable. When I click the A it should link me to another api url (the problem here is the api url is different for every name object) So, the api for

A = http://example/name?=A 
B = http://example/name?=B

Now, how do I link my name object content (A, B) to api (url) and make it fetch data and display as table on another page.

So, when I click on A it should send me to another url and on that webpage I should be able to see the fetched data of (that api url that I just got send to) in the table form.

My code:

     import React, { Component } from "react";

    export default class Stocks extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
      isLoaded: false,
      search: "",
    };
  }

  updateSearch(event) {
    this.setState({ search: event.target.value });
  }

  componentDidMount() {
    fetch("http://131.181.190.87:3001/all")
      .then((res) => res.json())
      .then((json) => {
        this.setState({
          isLoaded: true,
          items: json,
        });
      });
  }

  render() {
    let filteredItems = this.state.items.filter((item) => {
      return (
        item.symbol.toUpperCase().indexOf(this.state.search.toUpperCase()) !==
          -1 || item.industry.indexOf(this.state.search) !== -1
      );
    });
    var { isLoaded, items } = this.state;
    if (!isLoaded) {
      return <div>Loading...</div>;
    } else {
      return (
        <div>
          <form className="form-for-table-search">
            SelectSymbol: &emsp;
            <input
              type="text"
              value={this.state.search}
              onChange={this.updateSearch.bind(this)}
            />
            &emsp; &emsp;{" "}
            <button type="button" className="btn-submit">
              Search
            </button>
            <br />
            &emsp; Industry: &emsp; &emsp; &emsp;
            <input
              type="text"
              value={this.state.search}
              onChange={this.updateSearch.bind(this)}
            />
            <button type="button" className="btn-submit">
              Search
            </button>
            &emsp; &emsp; History of Symbol and date : &emsp; &emsp; &emsp;
            <input
              type="text"
              value={this.state.search}
              onChange={this.updateSearch.bind(this)}
            />
            &emsp; &emsp;
            <button type="button" className="btn-submit">
              Search
            </button>
          </form>
          <table border={2} cellPadding={1}>
            <thead>
              <tr>
                <th>Symbol</th>
                <th>Name</th>
                <th>Industry</th>
              </tr>
            </thead>

            <tbody>
              {filteredItems.map((item) => (
                <tr>
                  <td key={item.symbol}>{item.symbol}</td>
                  <td key={item.name}>{item.name}</td>
                  <td key={item.industry}>{item.industry}</td>
                </tr>
              ))}
              }
            </tbody>
          </table>
        </div>
      );
    }
  }
}

Any help is appreciated. Thanks.

Samuel Liew
  • 68,352
  • 105
  • 140
  • 225
Hemlata
  • 65
  • 6

1 Answers1

1

You can dynamically create your click handlers to pass the info you need to create the url for the fetch. Something like this:

handleClick = name => 
    fetch(`http://example.com/name?=${name}`).then((data) => {
        // handle redirect to new page
    })
<td key={item.name} onClick={() => handleClick(item.name)}>{item.name}</td>

I can't give a proper answer for how to redirect to your new page without knowing more about how your app is set up.

If you're using React Router, you could use a Redirect or history to pass the data to your display table component. Alternatively, you could render a Link in the table cell instead, passing the name as a prop to the new page component, and have that component fetch on mount.

shannon
  • 310
  • 2
  • 8
  • Well I tried this and I am able to fetch the data. I was wondering how can I pass that data to another class? so I can output the table on my webpage. – Hemlata May 10 '20 at 05:33
  • Like I said above, I'd need to know more about how your app is structured and where you intend to render the component displaying the data before I can answer that – shannon May 10 '20 at 05:46
  • Well my app is a stock app that gets the data from api and display it in the form of table. I can fetch the api like you said, I need to pass it to let say symbol.js class now (as right now its displaying in the console of stock.js class) and render the json data we received to that symbol.js page. So, now when we go to http:/..../symbol.js by it should display in its body all the data in table form (the data here is the one we fetched in your example and right now I am just displaying it in the console in stocks.js class). It should somehow pass the data to symbol.js class – Hemlata May 10 '20 at 05:52
  • And, moreover when I click on the item.name ( the table data) it should send me to symbol.js class and when I land on that page it should display the json data (the one I explained in last comment) – Hemlata May 10 '20 at 05:55
  • If you haven't implemented any kind of routing already I'd recommend including React Router in your project, adding a route at '/symbol' or (whatever you'd like to show up in the address bar) which renders your table component, and using the history functionality I mentioned in the answer above to redirect to that route and pass the data to the component using history's state object. – shannon May 10 '20 at 06:38
  • I have update my answer with screenshot. Please have a look – Hemlata May 10 '20 at 08:23
  • I'd recommend changing your question back to the original so that this question and answer will be useful to others, and asking a new question if you have specific questions about using react router. – shannon May 10 '20 at 15:06
  • I changed it a lot, so it's hard to change again, but I wrote on title (Edited) – Hemlata May 11 '20 at 06:00
  • And, please let me know if you have any answer for it. Thanks! – Hemlata May 11 '20 at 06:01