简体   繁体   English

如何正确编写React组件的包装器?

[英]How to properly write a wrapper for a React component?

Now that most your navigation is in place, you should make it so that the different pets are shown in the PetList . 现在大部分导航都已到位,您应该这样做,以便PetList中显示不同的宠物。 To do this, modify the / route in the App component so that instead of specifying a component property, you specify a render property. 为此,请修改App组件中的/ route,以便指定render属性,而不是指定component属性。 This property be assigned a callback function that takes in a set of props, and returns a <PetList> element with those props as well as the original pet prop. 该属性被赋予一个回调函数,该函数接收一组道具,并返回带有这些道具的<PetList>元素以及原始pet道具。

Tip: Try declaring this function as a local variable inside of the App 's render() function, and then passing that function value as a prop. 提示:尝试将此函数声明为Apprender()函数内的局部变量,然后将该函数值作为prop传递。 This can help with readability. 这有助于提高可读性。

Once this works, you should be able to see the list of pet cards when you visit the / route. 一旦这样做,你应该能够在访问/ route时看到宠物卡列表。

This is the main problem, of which I have solved how to route without the wrapper. 这是主要问题,我已经解决了如何在没有包装器的情况下进行路由。 The problem is once I try write this into a local variable I get lost. 问题是,一旦我尝试将其写入局部变量,我就会迷失方向。 A few questions I had include: 我有几个问题包括:

  • Where do I place this local variable? 我在哪里放置这个局部变量? It is placed before the return statement in the render function correct? 它放在render函数中的return语句之前是否正确?
  • The link above shows how to do this, however; 上面的链接显示了如何执行此操作; I do not know how to apply this to the situation 我不知道如何应用这种情况
  • I don't really understand ... and ...rest what is the purpose of ... alone as well as with ...rest ? 我真的不明白...而且...rest的目的是什么...单独以及...rest
render() {
  return (
    <div>
      <header className="jumbotron jumbotron-fluid py-4">
        <div className="container">
          <h1><Link to='/'>Adopt a Pet</Link></h1>
        </div>
      </header>

      <main className="container">
        <div className="row">
          <div className="col-3">
            <AboutNav />
          </div>
          <div className="col-9">
            <Switch>
              <Route exact path='/' render={() => 
                <PetList pets={this.state.pets} />
              } />
              <Route exact path='/about' component={AboutPage} />
              <Route exact path='/resources' component={ResourcesPage} />
              <Redirect to='/' />
            </Switch>
          </div>
        </div>
      </main>

      <footer className="container">
        <small>Images from <a href="http://www.seattlehumane.org/adoption/dogs">Seattle Humane Society</a></small>
      </footer>
    </div>
  );
}

Where do I place this local variable? 我在哪里放置这个局部变量? It is placed before the return statement in the render function correct? 它放在render函数中的return语句之前是否正确?

Yes. 是。 You have this: 你有这个:

render() {
  return (
    // ...
    <Route exact path='/' render={() => 
      <PetList pets={this.state.pets} />
    } />
    // ...
  );
}

The text is telling you to do something like this: 文本告诉你做这样的事情:

render() {
  const renderPetList = () => <PetList pets={this.state.pets} />;

  return (
    // ...
    <Route exact path='/' render={renderPetList} />
    // ...
  );
}

I don't really understand ... and ...rest what is the purpose of ... alone as well as with ...rest ? 我真的不明白...而且...rest的目的是什么...单独以及...rest

I can't tell what this is in reference to, but I'm guessing you've seen it in the context of object destructuring. 我不知道这是什么参考,但我猜你已经在对象解构的背景下看到了它。

This is a contrived example, but Suppose you wanted to create a wrapper for <button> that takes a bunch of props that you'll pass straight to the <button> and one that you won't (let's say " mySize "), because you want to do something else with it first. 这是一个人为的例子,但是假设你想为<button>创建一个包装器,它带有一些你直接传递给<button>的道具和一个你不会传递的道具(让我们说“ mySize ”),因为你想先用它做别的事。 You could do it this way: 你可以这样做:

const MyButton = ({ mySize, childen, disabled, tabIndex, onClick, children }) => {
  const className = mySize === 'big' ? 'big-button' : 'normal-button';

  return (
    <button className={className} disabled={disabled} tabIndex={tabIndex} onClick={onClick}>
      {children}
    </button>
  );
};

That's really verbose, though. 但这真的很冗长。 Instead of repeating the names of all of the props we want to pass to <button> , we can use ... (the "spread operator") to pick out certain props to put in variables to do with as we will while the rest (ergo ...rest , although you can call it whatever you want) go in an object, making it much more concise: 相反,重复全部,我们要传递给道具的名称<button> ,我们可以使用... (以下简称“传播经营者”),以挑选出一定的道具摆在与变量,我们将其余的做(ergo ...rest ,尽管你可以随意调用它)进入一个物体,使它更简洁:

const MyButton = ({ mySize, ...restProps }) => {
  const className = mySize === 'big' ? 'big-button' : 'normal-button';

  return (
    <button className={className} {...restProps}>
      {children}
    </button>
  );
};

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

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