简体   繁体   English

我怎样才能解构一个 React 道具并仍然访问其他道具?

[英]How can I destructure a React prop and still access other props?

I'm curious if the parameters(props) of a component can be de-structured similarly to an import if I want all the props but also to de-structure a single property.我很好奇,如果我想要所有的道具,但也要解构一个属性,那么组件的参数(道具)是否可以像导入一样解构。 I guess this is more of a JavaScript question than a React question, but for example.我想这更像是一个 JavaScript 问题而不是 React 问题,但是例如。

import React, {useEffect} from 'React'

I understand that an import is different than what I'm asking but I'm just using as an example.我知道导入与我要求的不同,但我只是举个例子。 Is something like the following possible?像下面这样的事情可能吗?

const props = {
  test: true,
  destructure: 'yes'
}

const TestComponent = (props, {desctructure}) => {
  return <div>Not Important, but not having a return bothered me enough to add it</div>;

I'm well aware that I can do this:我很清楚我可以这样做:

const TestComponent = (props) => {
  const { destructure } = props;
  return <div>Another return</div>;

My reasoning for why this would be useful:我为什么这会有用的理由:

When navigating through large and overly complex components, I feel like the first option would make reading props much easier as it would stick out that these values are coming from props.当浏览大型和过于复杂的组件时,我觉得第一个选项会使阅读 props 更容易,因为它会突出这些值来自 props。 Of course, a prop value could be written as props.destructure, but this almost feels like too much boilerplate if there's a lot of references.当然,一个 prop 值可以写成 props.destructure,但如果有很多引用,这几乎感觉像太多的样板。

I've been wondering this for a long time now and I really just want to make sure I'm not missing something simple here.很长一段时间以来,我一直在想这个问题,我真的只是想确保我没有遗漏一些简单的东西。 I'm perfectly fine for doing the latter and I'm not looking for some custom implementation on this.我非常适合做后者,我不是在寻找一些自定义实现。

The import example is a very different case.导入示例是一个非常不同的情况。 Exports are separated into the default and named exports, which can be done side by side.导出分为默认导出和命名导出,它们可以并排完成。

What you could do, since you want to keep most of your props in the props object, but use some outside of it, is to use an object rest operator to spread the rest of the props into one object ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax ):你可以做的,因为你想把大部分道具保留在道具 object 中,但使用它之外的一些道具,就是使用 object rest 运算符将道具的 rest 传播到一个 object( 881205833://5developer.6833://5 mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax ):

const TestComponent = ({destructure, ...props}) => {
  return <div>Another return</div>;
}

 const TestComponent = ({destructure, ...props}) => { console.log(destructure,props); return <div>Another return <br/> destructure: {JSON.stringify(destructure)} <br/> props: {JSON.stringify(props)} </div>; } ReactDOM.render(<TestComponent test destructure="yes" />,document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"/>

Sure.当然。 It's actually very straightforward to achieve this using ...rest object. In your case I called it props .使用...rest object 实现这一点实际上非常简单。在您的情况下,我将其称为props

const Comp = ({ prop1, ...props }) => {
  console.log(prop1); // cat
  console.log(props.prop2); // dog
  return null;
};

export default function App() {
  return (
    <div>
      <Comp prop1="cat" prop2="dog" prop3="cow" />
    </div>
  );
}

Just be aware that your props object will no longer include destructured prop1.请注意,您的道具 object 将不再包含解构的道具 1。

There's very a hacky way, by using a default argument:有一个非常 hacky 的方式,通过使用默认参数:

 const props = { test: true, destructure: 'yes' } const App = (props, _, {destructure} = props) => { console.log(props); console.log(destructure); return 'app'; }; ReactDOM.render(<App {...props} />, document.querySelector('.react'));
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div class="react"></div>

But it's very confusing.但这非常令人困惑。 I wouldn't recommend it.我不推荐它。 Better to destructure on the first line if needed.如果需要,最好在第一行进行解构。

Note that from the code in your question:请注意,从您问题中的代码中:

import React, {useEffect} from 'React'

While this looks similar to destructuring, it's something entirely different .虽然这看起来类似于解构,但它是完全不同的东西。 This is import syntax.这是导入语法。 Using a plain name when importing indicates to import the default export from the other module:导入时使用普通名称表示从其他模块导入默认导出:

import React

Using brackets indicates to import the named export from the module:使用括号表示从模块导入命名导出:

import {useEffect}

It's not destructuring.它不是破坏性的。

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

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