【问题标题】:React - How To Compare Props Between Separate ComponentsReact - 如何比较不同组件之间的 props
【发布时间】:2019-04-23 10:44:56
【问题描述】:

如何比较两个独立组件之间的 props 是否具有相同的值?

1- 我所寻求的可行吗?

2- 如果没有,我还能如何完成以下问题:

故事:

  • 我有一个汽车对象数组。 每辆车的名称在<CarList /> 组件上显示为<li />。 点击每个<li/> 就会显示汽车的颜色

  • 我有一个 <Question /> 组件,它呈现:“什么车是(这里是随机颜色)”?

界面变化: 我怎么能写一个方法:

  • 检查 <CarList /> 的 props.color 是否 === <Question /> 的 props.color
  • 然后它会触发 UI 更改,例如:
  • onClickIf 汽车的颜色与问题的颜色相匹配:将<li /> 更改为绿色(即:背景色),else 将其更改为红色。

我正在努力(想知道是否可能)比较来自不同组件的道具 + 编写一个检查和执行上述 UI 更改的方法。

这是反映上述解释的代码:也是sandbox

// Garage
export default class Garage extends Component {
  state = {
    cars: [
      { name: "Ferrari", color: "red", id: 1 },
      { name: "Porsche", color: "black", id: 2 },
      { name: "lamborghini", color: "green", id: 3 },
      { name: "McLaren", color: "silver", id: 4 },
      { name: "Tesla", color: "yellow", id: 5 }
    ]
  };

  handleShuffle = () => {
    this.setState({
      cars: [...this.state.cars.sort(() => Math.random() - 0.5)]
    });
  };

  render() {
    const { cars } = this.state;
    const car = cars.map(car => (
      <CarList key={car.id} make={car.name} color={car.color} />
    ));

    const guess = cars
      .slice(2, 3)
      .map(car => <Question key={car.id} color={car.color} />);
    return (
      <>
        <div>{guess}</div>
        <button onClick={this.handleShuffle}>load color</button>
        <ul>{car}</ul>
      </>
    );
  }
}

// CarList
class CarList extends Component {
  state = {
    show: false
  };

  handleShow = () => {
    this.setState({ show: true });
    console.log(this.props);
    // check for props equality here

    //desired result for <li /> would be
    // className={ correctColor ? 'green' : 'red'}
  };

  render() {
    console.log("car color props:", this.props.color);
    const { make, color } = this.props;
    const { show } = this.state;
    return (
      <li onClick={this.handleShow}>
        {make}
        <span className={show ? "show" : "hide"}>{color}</span>
      </li>
    );
  }
}

// Question
const Question = ({ color }) =>
  console.log("question color prop:", color) || <h1>What car is {color}</h1>;

【问题讨论】:

    标签: javascript reactjs ecmascript-6 react-props


    【解决方案1】:

    有很多方法可以做到这一点,但最简单的方法是将问题中的颜色发送到汽车组件。

    https://codesandbox.io/s/my4wmn427x

    【讨论】:

    • 谢谢!因此,通过这种方法,&lt;Question/&gt; 在哪里/如何知道questionColor 发送到&lt;CarList /&gt;
    • 不知道它的颜色正在发送到 。在这种方法中,车库知道这两种颜色,并选择另外将问题颜色发送到 以便每辆汽车都可以与自己的汽车颜色进行比较:)
    【解决方案2】:

    是的,您可以将正确的颜色传递给CarList 组件或CarList 是否正确的标志。检查我的沙盒。

    https://codesandbox.io/s/92xnwpyq6p

    基本上,我们可以将isCorrect 属性添加到CarList,它的值为correctCar.color === car.color,然后我们使用它来确定我们应该将其渲染为绿色还是红色。

    【讨论】:

    • 非常感谢您的解释和代码示例。理想情况下,红色或绿色仅在单击时触发。在此之前,它应该留在black。我会/应该在className={.....} 上按摩这个逻辑吗?
    • 在这种情况下您可以使用空字符串。我想我也在我的沙箱中使用它。看看吧
    • 检查我的 getColor() 函数
    • Garage.js 第 62 行正确索引:Math.random() 返回一个介于 0 和 1 之间的随机数。我将它乘以汽车的长度减 1,表示如果长度为 5,则随机数将是 0 到 4 之间的随机数。因为我想要一个整数,所以我调用 Math.floor。
    • 哦,我忘记点击保存了,你现在应该可以看到了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多