React: component only rendering new values on second onClick of button

damtypo

I making a react app and I have a parent component Search with child components Input and Result. Input has a drop down menu which passes a value, genreValue, to Search, through a callback function when a button is clicked. Search then makes an api call, which works fine.

My problem is it takes two clicks of the button for the new API data to render. Looking at other SO questions I suspect I need to pass genreValue as an argument to the cb function, or my onClick is only initialising, rather than invoking it on the first click.

It's a pretty simple app so I wouldn't think Flux etc would be needed. My console logs seem to show the value being changed in the Search and Input components. So what am I doing wrong?

Search.js

let Search = React.createClass ({
  getInitialState(){
    return {
      movies: ['Men In Black'],
      genreValue: '12'
    };
  },

  componentDidMount(){
    this.getMovies()
  },

  getMovies(){
    let genre = this.state.genreValue;
    let url = `http://api.themoviedb.org/3/discover/movie?${key}&with_genres=${genre}`;
    Request.get(url).then((response) => {
      console.log('response.body.results', response.body.results)
      this.setState({
        movies: response.body.results.map(function(movie){
          return movie.title
        })
      });
    });
  },

  handleGenre(newGenre) {
    this.setState({ genreValue: newGenre })
    return this.getMovies();
  },

  render(){
      console.log(this.state.movies)
      console.log('genreValue state', this.state.genreValue)
      return (
        <div>
          <Input genre={this.state.genreValue} onGenreChanged={this.handleGenre}/>
          <ul>
            {this.state.movies.map( function(movie){
              return <Results key={movie.id} data={movie}/>;
            })}
          </ul>
        </div>
      );
  }

});

export default Search;

Input.js

let Input = React.createClass ({

  selectHandler(){
    return this.props.onGenreChanged(this.refs.genre.value);
  },

  render() {
    console.log('genreValue prop', this.props.genre);
    console.log('refs', this.refs.genre)
      return <div>
      <select ref="genre">
        <option value="28">Action</option>
        <option value="12">Adventure</option>
        <option value="16">Animation</option>
        <option value="35">Comedy</option>
        <option value="80">Crime</option>
        <option value="99">Documentary</option>
        <option value="18">Drama</option>
        <option value="10751">Family</option>
        <option value="14">Fantasy</option>
        <option value="10769">Non-english</option>
        <option value="36">History</option>
      </select>

      <button onClick={this.selectHandler} value="Go">Go</button>
      </div>
    }
});

export default Input;
Ben Nyberg

In the handleGenre function, state may not have updated when this.getMovies is called. You could change it to the following:

handleGenre(newGenre) { this.setState({ genreValue: newGenre }, function() { return this.getMovies(); }); },

Or, probably better practice would be to call this.getMovies in a componentDidUpdate lifecycle function if genreValue has changed:

componentDidUpdate: function(prevProps, prevState) { if (prevState.genreValue !== this.state.genreValue) { this.getMovies(); } }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Dynamically Rendering a React component

From Java

Append New React Component onclick

From Dev

Modal rendering and onClick in React

From Dev

React onClick event on component

From Dev

React, onClick only works after second click, Axios, Async/Await

From Dev

Simple React component not rendering

From Dev

Onclick Button open a new Window only with Image inside

From Dev

React - server side component rendering

From Dev

React router not rendering component

From Dev

onClick doesn't render new react component.

From Dev

React component only changes state on second onClick event

From Dev

Rendering new component on-click in a parent component

From Dev

React server rendering --> Replacing React-rendered children with a new root component

From Dev

React Js component rendering only once after state change

From Dev

React — onClick in component instance?

From Dev

OnClick button working only after second time Asp.NET c#

From Dev

Controlling Order of React Component Rendering

From Dev

Handling an onClick on a component in React

From Dev

Second onClick listener in a new activity

From Dev

React - server side component rendering

From Dev

OnClick button working only after second time Asp.NET c#

From Dev

Component rendering with React in Rails not working

From Dev

React router not rendering component

From Dev

Method in React component not rendering HTML

From Dev

React not rendering component

From Dev

Rendering a Component Instance in React

From Dev

Basic react component not rendering

From Dev

Rendering Math in React Component

From Dev

React--Button onclick event not working in Component

Related Related

HotTag

Archive