【问题标题】:Star Rating buttons not rendering as expected using React.JS使用 React.JS 未按预期呈现星级按钮
【发布时间】:2020-12-22 20:41:09
【问题描述】:

所以我的星形按钮显示在我的 UI 上。正如预期的那样,但没有渲染。我可以通过在状态函数中更改 CurrentRating 的整数值来更改它们的输出,无论我将其更改为 1、2、3 等。但就是这样。

点击任何按钮后,该按钮上的星号和它前面的星号都应该变成红色。

我错过了什么?

这是我的代码:

card.js ///

 import React, { Component } from "react";


 import { Stars } from "./stars";

 export class Card extends Component {
 // Initiatte clicks at 0
 constructor(props) {
   super(props);
   this.state = {


   CurrentRating: 0,
 };
}

// Rate your instructor
handleRating = (value) => {
  console.log("Rating declared");
this.setState({ CurrentRating: value });
};
///

render() {
  // Create a variable for Star rating
  const Rating = this.state.CurrentRating;

  return (
  <React.Fragment>
    <div className="container">
      

      <div className="stars">
        <p>Your rating is {Rating}</p>
        <Stars rating={Rating} onClicked={this.handleRating} />
      </div>
    </div>
  </React.Fragment>
  );
 }
}

export default Card;

stars.CSS ///

.active {
color: red;
}

stars.js ///

import React, { Component } from "react";
import "./stars.css";


export class Stars extends Component {
///
render() {
// Sample props
  const Rating = this.props.rating;
  console.log("[Stars] Render, Rating=" + Rating);

  /// Render or Genrate markup.
  return (
  <div>
    {Rating >= 1 && (
      <button
        className="active"
        onClick={this.onClicked}
        style={{ cursor: "pointer" }}
      >
        ★
      </button>
    )}

    {Rating < 1 && (
      <button onClick={this.onClicked} style={{ cursor: "pointer" }}>
        ★
      </button>
    )}

    {Rating >= 2 && (
      <button
        className="active"
        onClick={this.onClicked}
        style={{ cursor: "pointer" }}
      >
        ★
      </button>
    )}

    {Rating < 2 && (
      <button onClick={this.onClicked} style={{ cursor: "pointer" }}>
        ★
      </button>
    )}

    {Rating >= 3 && (
      <button
        className="active"
        onClick={this.onClicked}
        style={{ cursor: "pointer" }}
      >
        ★
      </button>
    )}

    {Rating < 3 && (
      <button onClick={this.onClicked} style={{ cursor: "pointer" }}>
        ★
      </button>
    )}

    {Rating >= 4 && (
      <button
        className="active"
        onClick={this.onClicked}
        style={{ cursor: "pointer" }}
      >
        ★
      </button>
    )}

    {Rating < 4 && (
      <button onClick={this.onClicked} style={{ cursor: "pointer" }}>
        ★
      </button>
    )}

    {Rating >= 5 && (
      <button
        className="active"
        onClick={this.onClicked}
        style={{ cursor: "pointer" }}
      >
        ★
      </button>
    )}

    {Rating < 5 && (
      <button onClick={this.onClicked} style={{ cursor: "pointer" }}>
        ★
      </button>
    )}
   </div>
  );
 }
}

App.js ///

 import './App.css';

 import { Card } from './card';

 function App() {
   return (
     <div className="App">
     <header className="App-header">
   
      <Card />
   
    </header>
  </div>
  );
 }

 export default App;

【问题讨论】:

    标签: javascript html css reactjs


    【解决方案1】:

    问题在于单击按钮时调用的函数

     <button onClick={this.onClicked} 
    

    首先,这应该是this.props.onClicked,因为函数作为道具传入(而不是在 Stars 类本身上)。

    其次,您需要在函数中传递评分的实际值。即

     <button onClick={() => this.props.onClicked(1)} 
    

    如果我在第一个按钮上进行上述更改并单击它,它会变成红色

    【讨论】:

    • 看起来很棒!感谢您的帮助!
    • 但是对于我的 Like Increment Button,我只需要使用 onClick={this.props.onLike},而不使用 () =>。为什么我必须对星形按钮使用 () => 而没有我的 Like Increment 按钮?
    • 没有看到你喜欢的增量按钮,我的假设是当你调用函数时它不需要参数。本质上,当你有类似 onClick={this.props.onLike} 的东西时,React 将你的函数调用为 this.props.onLike(event) 其中 event 包含有关事件本身的一堆信息(目标元素等)
    • 但是在你的 onClicked 函数的情况下,你不想要事件,你想要传递星的数值(这可以通过事件推断,但我不会现在进入)。实际上,对 onClick 的更改正在执行 onClick={(event) => this.props.onClicked(1)} 但我们不使用该事件,因此丢弃它。
    • 是的,对于我的 Like Increment 按钮来说确实如此,我在事件中使用了“目标”。总体感谢您提供的信息,下次我会记住的。
    猜你喜欢
    • 2017-02-23
    • 2018-10-01
    • 2014-09-29
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2021-03-12
    • 2016-12-14
    相关资源
    最近更新 更多