Giter VIP home page Giter VIP logo

Comments (7)

dreyescat avatar dreyescat commented on August 19, 2024

Uhm... It is weird. I tried this:

<Rating onChange={rate => alert(rate)} />

It simply alerts when the rate changes. It seems to work. Check this codepen. No need to click twice.

from react-rating.

dreyescat avatar dreyescat commented on August 19, 2024

Hey @MrAbalogu, could you try if the new release 1.0.5 solves this issue? I couldn't reproduce your behaviour. Could you give me a reproducible scenario? Nothing fancy. Something that helps me reproduce it, like issue #76.

from react-rating.

MariaRoblesSpin avatar MariaRoblesSpin commented on August 19, 2024

I have exactly the same problem. But you're right, @dreyescat. If I try this <Rating onChange={rate => alert(rate)} /> the alert shows me the correct number. But If I put my handleRating function it works wrong... this is my handleRating function

_handleRating = (value) => {
    try {
      const { rating } = this.state
      this.setState({ rating: value })
      const movie = this.props.movie
      console.log('Rating value in handleRating: ', rating)
      console.log('Movie value in handleRating: ', movie)
      this.props.onRating({ rating, movie })
    } catch (error) {
      console.log('error in handleRating: ', error)
    }
  }

There is a mistake somewhere but I'm not able to find it.
Could you help me?
Thank you in advance.

from react-rating.

dreyescat avatar dreyescat commented on August 19, 2024

@MariaRoblesSpin I think you are showing the previous value, not the current one.

const { rating } = this.state

This statement is assigning the previous rating value to the rating const variable. The new one is in the value parameter of the _handleRating function.

Maybe you think that the next setState call is updating this local variable... But it isn't.

from react-rating.

dreyescat avatar dreyescat commented on August 19, 2024

I am not sure what you need... But if you want to call a callbag once the setState has completed I suggest using the componentDidUpdate method:

  componentDidUpdate() {
    const { rating } = this.state;
    const { movie } = this.props;
    this.props.onRating({ rating, movie});
  }

  _handleRating = (value) => {
    this.setState({ rating: value });
  }

Check this codesandbox

from react-rating.

Falci avatar Falci commented on August 19, 2024

Same issue happened to me.
I believe it happens when the onChange forces a re-render in the Rating component.

Here's a small reproducible example.

from react-rating.

Falci avatar Falci commented on August 19, 2024

Here's a small reproducible example.

Btw, I managed to solve it by adding the initialRating:

<Rating onChange={setRate} initialRating={rate} />

from react-rating.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.