0

I am trying to display list dynamically when user click on input box. for that I took onChange event handle on input box and setting state to new data when user click on input box. but it is not giving me desired result. can anyone help me to solve the issue ? When user click on input box then only list should be displayed but in my case it's displaying already.

SearchBox.js

import React, { Component } from "react";
import SourceData from "../assets/continents.json";

class SearchBox extends Component {
  state = {
    value: ""
  };

  handleChange = e => {
    this.setState({
      sourceData: SourceData
    });
  };

  render() {
    const searhBox = (
      <input type="text" value={this.state.value} onClick={this.handleChange} />
    );
    const selectBox2 = SourceData.map(option => <li>{option.continent}</li>);

    return (
      <React.Fragment>
        <h2>Step 1</h2>
        <h3>Select a continent.</h3>
        {searhBox}
        <ul>{selectBox2}</ul>
      </React.Fragment>
    );
  }
}

export default SearchBox

continents.json

[
  {
    "continent": "Africa",
    "countries": [
      {
        "name": "Nigeria",
        "flag": "ð³ð¬"
      },
      {
        "name": "Ethiopia",
        "flag": "ðªð¹"
      },
      {
        "name": "Egypt",
        "flag": "ðªð¬"
      },
      {
        "name": "DR Congo",
        "flag": "ð¨ð©"
      },
      {
        "name": "South Africa",
        "flag": "ð¿ð¦"
      }
    ]
  },
  {
    "continent": "America",
    "countries": [
      {
        "name": "USA",
        "flag": "ðºð¸"
      },
      {
        "name": "Brazil",
        "flag": "ð§ð·"
      },
      {
        "name": "Mexico",
        "flag": "ð²ð½"
      },
      {
        "name": "Colombia",
        "flag": "ð¨ð´"
      },
      {
        "name": "Argentina",
        "flag": "ð¦ð·"
      }
    ]
  },
  {
    "continent": "Asia",
    "countries": [
      {
        "name": "China",
        "flag": "ð¨ð³"
      },
      {
        "name": "India",
        "flag": "ð®ð³"
      },
      {
        "name": "Indonesia",
        "flag": "ð®ð©"
      },
      {
        "name": "Pakistan",
        "flag": "ðµð°"
      },
      {
        "name": "Bangladesh",
        "flag": "ð§ð©"
      }
    ]
  }

 ]

output ::

enter image description here

1 Answer 1

2

In SearchBox.render, build up the list of countries from this.state.sourceData

const selectBox2 = this.state.sourceData.map(option => <li>{option.continent}</li>);

return (
  <React.Fragment>
    <h2>Step 1</h2>
    <h3>Select a continent.</h3>
    {searhBox}
    {selectBox2 && <ul>{selectBox2}</ul>}
  </React.Fragment>
);

Also, remember to set an initial value for sourceData in SearchBox.state.

state = {
   value: '',
   sourceData: []
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it's working... to make all items clickable should I use li or select box ?
That depends on the UX you're driving to implement here. I'd use select box and make the list items options in it and then remove the input field. However from the mock you've shared, I'd make the list items clickable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.