简体   繁体   English

React-如何在各个组件之间比较道具

[英]React - How To Compare Props Between Separate Components

How can I compare if the props between two separate components have the same value? 如果两个单独的组件之间的道具具有相同的值,我该如何比较?

1- Is what I'm seeking doable? 1-我寻求的是可行的吗?

2- If not, how else could I accomplish the ask below: 2-如果没有,我还能如何完成以下要求:

The story: 故事:

  • I have an array of car objects. 我有一系列汽车用品。 Each car's name is displayed as <li /> on a <CarList /> component. 每辆汽车的名称在<CarList />组件上显示为<li /> Upon click on each <li/> the car's color is revealed 单击每个<li/> ,就会显示汽车的颜色

  • I have a <Question /> component that renders: "What car is (random color here)"? 我有一个<Question />组件,该组件呈现:“什么车(这里颜色随机)”?

UI change: How could I write a method that: 使用者介面变更:我该如何写一个方法:

  • Checks if the props.color of <CarList /> === the props.color of <Question /> 检查<CarList />的props.color === <Question />的props.color
  • Then it fires a UI change such as: 然后触发UI更改,例如:
  • onClick : If the car's color matches the question's color: change the <li /> to green (ie: background-color), else change it to red. onClickIf汽车的颜色与问题的颜色匹配:将<li />更改为绿色(即:background-color), else将其更改为红色。

I'm struggling (wondering if it's possible) to compare props from different components + writing a method that checks and executes the UI change above. 我正在努力(想知道是否可能)比较来自不同组件的道具,并编写了一种方法来检查并执行上面的UI更改。

This is the code reflecting the explanation above: Also here's the 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>;

Yes, you can pass the correct color to the CarList component or the flag whether the CarList is a correct one. 是的,您可以将正确的颜色传递给CarList组件或标志,无论CarList是否正确。 Check my sandbox. 检查我的沙箱。

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

Basically we can add isCorrect prop to CarList which has value of correctCar.color === car.color and we use it to determine whether we should render it green or red. 基本上,我们可以添加isCorrect道具CarList具有的价值correctCar.color === car.color ,我们用它来确定我们是否应该使其绿色或红色。

Theres many ways to do this but the simplest is to send the color in the question down to the car component. 有很多方法可以做到这一点,但最简单的方法是将问题中的颜色发送到汽车部件。

https://codesandbox.io/s/my4wmn427x https://codesandbox.io/s/my4wmn427x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM