score:1
Accepted answer
componentDidMount
get called only once when your component get attached to the page. So it's not the correct place to call you search API. Instead, you should call it every time when the user clicks 'submit' button. For that, you need to bubble handleSubmit
trigger to App component by passing a callback method as a prop to SearchForm component. Also, you don't need to use ref
as you already have search text in your state
.
SearchForm
class SearchForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
event.preventDefault();
if(this.props.onSubmit && typeof this.props.onSubmit === "function"){
this.props.onSubmit(this.state.value);
}
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input className="movieName" type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
<h1>{this.state.value}</h1>
</form>
);
}
}
App
class App extends React.Component { /* I'm not sure why you extends NameForm and what NameForm does */
constructor(props) {
super(props);
this.state = {
movie:[]
};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(value) {
let searchInput = value // you get the value of movieName input here
let sortByPop = "&sort_by=popularity.desc";
let requestUrl = 'https://api.themoviedb.org/3/search/movie?api_key=f8c4016803faf5e7f424abe98a04b8d9&query=' + searchInput + sortByPop;
axios.get(requestUrl).then(response => {
this.setState({movie: response.data.results})
});
}
render() {
let baseImgURL = "https://image.tmdb.org/t/p/w185_and_h278_bestv2";
let posterImgPath = this.state.movie.map(movie => movie.poster_path);
let posterLink = baseImgURL + posterImgPath;
// I'm not sure why use need above code as you don't use it anywhere
return(
<div className="App">
<Header />
<SearchForm onSubmit={this.handleSubmit}/>
<div>
{this.state.movie.map(movie =>
<div className="movieTitle">
<div className="movieCard">
<img className="posterImg" src= {`https://image.tmdb.org/t/p/w185_and_h278_bestv2/${movie.poster_path}`} alt={movie.title} />
<div className="searchFilmTitles" key={movie.id}>{movie.title}</div>
</div>
</div>
)}
</div>
</div>
);
}
}
Source: stackoverflow.com
Related Query
- React : how to get input value from one component to make the ajax call in another component?
- In React JS, how to get value from multiple input fields when one of them is changed
- How to get the value from button group in react component
- how to calculate the value from one input without eval() in react
- How to get the value from one component(Child Component) to another component (Parent Component) in reactjs?
- How to pass a dynamic value from one component to the other in React
- How do I get the value from a React Img component
- How to Make A second API call based on the value gotten from the first. with React and useEffect hooks
- how do I call or pass the value from one js file to another file in react js
- How to get input textbox value in parent file from component in React Js
- How to get value from a react function within a react component
- How to call a component function on other component, but from the other component ? React
- How to pass input value from child to parent component react
- How can I get the value from an animated or interpolated value in React Native?
- How to properly make a GET call in React returning an observable (resembling the method in Angular and not using promises)?
- How to get the selected value from a radio button group in React where there are multiple groups?
- How to make AJAX GET request on the client to external site with React
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How do I combine two arrays in react to get a new one with all of the items from the previous two?
- How should i get the value from custom attribute in react element?
- how to get a Component variable from another component but their both in the same file React
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to take the input from a react component and pass the length of the input to another react component
- Get React Hook Value from outside the functional component
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- How to get the value of selected option from React hooks?
- How to get input value from child component to my parent component on click of a button?
- how to get current value from input field in react js
- How to get the value of react component - AceEditor
- How can I get more than one value from the Redux store in a single statement?
More Query from same tag
- React Router transition in and out events
- React: What does following mean: import register, * as fromRegister from './registerReducer'?
- React: How to get value out of function and write it in state?
- What type is register from react-hook-form?
- ReactJS useEffect dependency warning leads to infninite loop
- How to add an onload without a fetch call in a function based component
- JSX element type 'Element[]' is not a constructor function for JSX elements?
- React Native navigating between Nested StackNavigator
- How do I convert a string to jsx?
- Proper way to wait for a function to finish or data to load before rendering in React?
- how to change the font color, font size, and button color in React
- React - Add user to list of Favorites
- React hook error when changing size of rendered array
- How can I import Audio into My Create-React-App application?
- Export React.JS variable and use in other file
- Video.js Playlists in React
- Settimeout not clearing in react hooks- function component
- this keyword behavior in classes and react component ( Arrow function vs regular function)
- How to create a single Gatsby Page to show and filter all blog posts by tag/category
- Passing props to children in React
- Config files for different production builds React
- How to trigger a cell change from within a component to update the grid data? (AG Grid - React)
- React : Best way to put dummy content in a component
- onClick gets fired immediately on component mount using firestore and React, redux
- Not able to fetch data from API endpoint in ReactJS?
- how to get id of object from firebase database in reactjs
- react js form inputs using eval?
- How to access firebase auth userMetadata
- How do I make UseEffect hook run step by step and not last?
- My React app is not registering setupProxy.js with http-proxy-middleware