Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

toggling individual item using map in react

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      loading: true,
      clicked: null // <-- start with null state
    }
  }
  
  componentDidMount() {
    fetch('https://restcountries.eu/rest/v2/all')
      .then(response => response.json())
      .then(data => this.setState({ data }))
      .finally(() => this.setState({ loading: false }));
  }

  clickHappens = (id) => () => {
    this.setState(prevState => ({
      clicked: prevState.clicked === id ? null : id, // <-- toggle back to null or to new id
    }));
  }
  
  render() {
    return (
      <div className="container">
        {this.state.data?.map((item, id) => (
          <div
            className="box"
            key={id}
            onClick={this.clickHappens(id)} // <-- pass id to toggle
          >
            {this.state.clicked === id ? // <-- check id match
              <Countryinformation
                continent={item["subregion"]}
                language={item["languages"][0]["name"]}
              />
              :
              <Countryname name={item["name"]}/>
            }
          </div>
        ))}
      </div>
    )
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react-native-wagmi-charts 
Javascript :: react export multiple components from index 
Javascript :: mongodb-nodejs-driver-deprecationwarning-collection-count-is-deprecated 
Javascript :: how to check my javascript code 
Javascript :: jquery keypress div color change 
Javascript :: useMediaquery hook react 
Javascript :: js cyclic motion based on cosine 
Javascript :: how to find the GCD in javascript 
Javascript :: The complete map() method syntax 
Javascript :: copy array using spread operator 
Javascript :: getderivedfromstate alternative 
Javascript :: angular universal prerender 
Javascript :: check if content is overflowing react 
Javascript :: Angular /Javascript- How can I shrink Sticky header on scroll functionality 
Javascript :: how to use same component in multiple place with some logic in angularjs 
Javascript :: angularjs Prevent from getting rendered 
Javascript :: angularjs NodeJS server performs POST request, but returns HTTPErrorResponse 
Javascript :: AngularJS Form validation transition to valid when some elements are not even touched yet 
Javascript :: want the app to save the passing screen after a user has passed the test even when the app exits in react native 
Javascript :: RegEx Pattern Validations failing on html input 
Javascript :: javascript get value outside function 
Javascript :: error first callback in node js 
Javascript :: saves javascript 
Javascript :: iterate over element parent jquery 
Javascript :: javascript looping through array 
Javascript :: creating hashblock method using sha256 hashing algorithm 
Javascript :: Self Invoking Function Tip 
Javascript :: react console logs not working 
Javascript :: how to broadcast to the entire room scket io 
Javascript :: var oddOrEven = function(num) {}; 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =