API - to simulate conditional rendering based on dummy API return value

0 votes
31 views
added Feb 26 in API by lcjr First Warrant Officer (11,830 points)

The simulation of API value can be change manually to rendering the elements. Change the value to 1,2,3 on this. For example, the below value are using 2. 

const [apiResponse, setApiResponse] = useState('2'); 

Thus, it'll rendering this under buddyPrompt_answer

{apiResponse === '2' && <BuddySearchListing />}

 

Here's the full code

import React, { useState, useEffect } from 'react';
import BuddySearchText from './search/BuddySearchText';
import BuddySearchListing from './search/BuddySearchListing';
import BuddySearchAgent from './search/BuddySearchAgent';

const BuddyPromptResult = () => {
  // State to simulate API response
  const [apiResponse, setApiResponse] = useState('2'); 

  // Function to manually set the API response
  const setManualApiResponse = (response) => {
    setApiResponse(response);
  };

  return (
    <div className="buddyPrompt_answer">
      {apiResponse === '1' && <BuddySearchText />}
      {apiResponse === '2' && <BuddySearchListing />}
      {apiResponse === '3' && <BuddySearchAgent />}
    </div>
  );
};

export default BuddyPromptResult;
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...