mirror of
https://github.com/FranP-code/countries.git
synced 2025-10-13 00:02:15 +00:00
Update React example to use AC3 and hooks
This commit is contained in:
@@ -10,25 +10,24 @@ This example uses [React](https://reactjs.org/) and [Apollo GraphQL](https://apo
|
|||||||
## Install dependencies
|
## Install dependencies
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ npm install react react-dom react-apollo apollo-boost graphql graphql-tag
|
$ npm install react react-dom @apollo/client graphql
|
||||||
```
|
```
|
||||||
|
|
||||||
## Build the component
|
## Build the component
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import ApolloClient from 'apollo-boost';
|
import React, {useState} from 'react';
|
||||||
import React, {Component} from 'react';
|
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import gql from 'graphql-tag';
|
import {ApolloClient, InMemoryCache, gql, useQuery} from '@apollo/client';
|
||||||
import {Query} from 'react-apollo';
|
|
||||||
|
|
||||||
// initialize a GraphQL client
|
// initialize a GraphQL client
|
||||||
const client = new ApolloClient({
|
const client = new ApolloClient({
|
||||||
|
cache: new InMemoryCache(),
|
||||||
uri: 'https://countries.trevorblades.com'
|
uri: 'https://countries.trevorblades.com'
|
||||||
});
|
});
|
||||||
|
|
||||||
// write a GraphQL query that asks for names and codes for all countries
|
// write a GraphQL query that asks for names and codes for all countries
|
||||||
const GET_COUNTRIES = gql`
|
const LIST_COUNTRIES = gql`
|
||||||
{
|
{
|
||||||
countries {
|
countries {
|
||||||
name
|
name
|
||||||
@@ -37,36 +36,24 @@ const GET_COUNTRIES = gql`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// create a component that renders an API data-powered select input
|
// create a component that renders a select input for coutries
|
||||||
class CountrySelect extends Component {
|
function CountrySelect() {
|
||||||
state = {
|
const [country, setCountry] = useState('US');
|
||||||
country: 'US'
|
const {data, loading, error} = useQuery(LIST_COUNTRIES, {client});
|
||||||
};
|
|
||||||
|
|
||||||
// set the selected country to the new input value
|
if (loading || error) {
|
||||||
onCountryChange = event => {
|
return <p>{error ? error.message : 'Loading...'}</p>;
|
||||||
this.setState({country: event.target.value});
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return (
|
|
||||||
<Query query={GET_COUNTRIES} client={client}>
|
|
||||||
{({loading, error, data}) => {
|
|
||||||
if (loading) return <p>Loading...</p>;
|
|
||||||
if (error) return <p>{error.message}</p>;
|
|
||||||
return (
|
|
||||||
<select value={this.state.country} onChange={this.onCountryChange}>
|
|
||||||
{data.countries.map(country => (
|
|
||||||
<option key={country.code} value={country.code}>
|
|
||||||
{country.name}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</select>
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
</Query>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<select value={country} onChange={event => setCountry(event.target.value)}>
|
||||||
|
{data.countries.map(country => (
|
||||||
|
<option key={country.code} value={country.code}>
|
||||||
|
{country.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.render(<CountrySelect />, document.getElementById('root'));
|
ReactDOM.render(<CountrySelect />, document.getElementById('root'));
|
||||||
|
|||||||
Reference in New Issue
Block a user