Skip to content

Commit ff4ae86

Browse files
committed
Ref a DOM Element
1 parent 1ee4c81 commit ff4ae86

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

src/App.js

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react';
22
import axios from 'axios';
3+
import PropTypes from 'prop-types';
34
import './App.css';
45

56
const DEFAULT_QUERY = 'redux';
@@ -159,22 +160,36 @@ class App extends Component {
159160
}
160161
}
161162

162-
const Search = ({
163-
value,
164-
onChange,
165-
onSubmit,
166-
children
167-
}) =>
168-
<form onSubmit={onSubmit}>
169-
<input
170-
type="text"
171-
value={value}
172-
onChange={onChange}
173-
/>
174-
<button type="submit">
175-
{children}
176-
</button>
177-
</form>
163+
class Search extends Component {
164+
componentDidMount() {
165+
if (this.input) {
166+
this.input.focus();
167+
}
168+
}
169+
170+
render() {
171+
const {
172+
value,
173+
onChange,
174+
onSubmit,
175+
children
176+
} = this.props;
177+
178+
return (
179+
<form onSubmit={onSubmit}>
180+
<input
181+
type="text"
182+
value={value}
183+
onChange={onChange}
184+
ref={el => this.input = el}
185+
/>
186+
<button type="submit">
187+
{children}
188+
</button>
189+
</form>
190+
);
191+
}
192+
}
178193

179194
const Table = ({ list, onDismiss }) =>
180195
<div className="table">
@@ -204,10 +219,22 @@ const Table = ({ list, onDismiss }) =>
204219
)}
205220
</div>
206221

222+
Table.propTypes = {
223+
list: PropTypes.arrayOf(
224+
PropTypes.shape({
225+
objectID: PropTypes.string.isRequired,
226+
author: PropTypes.string,
227+
url: PropTypes.string,
228+
num_comments: PropTypes.number,
229+
points: PropTypes.number,
230+
})
231+
).isRequired,
232+
onDismiss: PropTypes.func.isRequired,
233+
};
207234

208235
const Button = ({
209236
onClick,
210-
className = '',
237+
className,
211238
children,
212239
}) =>
213240
<button
@@ -218,6 +245,16 @@ const Button = ({
218245
{children}
219246
</button>
220247

248+
Button.defaultProps = {
249+
className: '',
250+
};
251+
252+
Button.propTypes = {
253+
onClick: PropTypes.func.isRequired,
254+
className: PropTypes.string,
255+
children: PropTypes.node.isRequired,
256+
};
257+
221258
export default App;
222259

223260
export {

0 commit comments

Comments
 (0)