`$` 在 React 组件的 props 中有什么作用?

huangapple go评论81阅读模式
英文:

what does the '$' do in React component props?

问题

I am studying styled-component, and I faced some problems in 'adapting based on props' part.

import './App.css';
import styled from 'styled-components';

const PrimaryButton = styled.button`
  color: ${props => props.primary ? "white" : "black"};
  background-color: ${props => props.primary ? "blue" : "gray"};
`;

function App() {
  return (
    <div>
      <PrimaryButton>Normal</PrimaryButton>
      <PrimaryButton primary>Primary</PrimaryButton>
    </div>
  );
}

export default App;

At first, I tried with this code, and the console told me to use primary=true, so I did.

But there was a following error in the console which says 'it looks like an unknown prop "primary" is being sent through to the DOM'. To solve this problem, I briefly scanned the styled-components document. I found out that they use $primary instead of primary. So the final code looks like this:

import './App.css';
import styled from 'styled-components';

const PrimaryButton = styled.button`
  color: ${props => props.$primary ? "white" : "black"};
  background-color: ${props => props.$primary ? "blue" : "gray"};
`;

function App() {
  return (
    <div>
      <PrimaryButton>Normal</PrimaryButton>
      <PrimaryButton $primary>Primary</PrimaryButton>
    </div>
  );
}

export default App;

I could no longer see the errors, but still I don't know the reason it happened and is solved.

What does $ mean?

英文:

I am studying styled-component, and I faced some problems in 'adapting based on props' part.

import &#39;./App.css&#39;;
import styled from &#39;styled-components&#39;

const PrimaryButton = styled.button`
  color: ${props=&gt;props.primary?&quot;white&quot;:&quot;black&quot;};
  background-color: ${props=&gt;props.primary?&quot;blue&quot;:&quot;gray&quot;};
`;

function App() {
  return (
    &lt;div&gt;
      &lt;PrimaryButton&gt;Normal&lt;/PrimaryButton&gt;
      &lt;PrimaryButton primary&gt;Primary&lt;/PrimaryButton&gt;
    &lt;/div&gt;
  );
}

export default App;

At first, I tried with this code, and the console told me to use primary=true, so I did.

But there was a following error in the console which says 'it looks like an unknown prop "primary" is being sent through to the DOM'. To solve this problem, I briefly scanned the styled-components document. I found out that they use $primary instead of primary. So the final code looks like this:

import &#39;./App.css&#39;;
import styled from &#39;styled-components&#39;

const PrimaryButton = styled.button`
  color: ${props=&gt;props.$primary?&quot;white&quot;:&quot;black&quot;};
  background-color: ${props=&gt;props.$primary?&quot;blue&quot;:&quot;gray&quot;};
`;

function App() {
  return (
    &lt;div&gt;
      &lt;PrimaryButton&gt;Normal&lt;/PrimaryButton&gt;
      &lt;PrimaryButton $primary&gt;Primary&lt;/PrimaryButton&gt;
    &lt;/div&gt;
  );
}

export default App;

I could no longer see the errors, but still I don't know the reason it happened and is solved.

What does $ mean?

答案1

得分: 5

这些用于 styled-components,与 React 无关。<br>
>如果你想阻止传递给底层 React 节点或渲染到 DOM 元素的属性是供 styled-components 使用的,请在属性名前加上美元符号($),将其转换为瞬态属性。

你可以在这里了解更多信息,也可以找到一个示例。

英文:

These are used by styled-components and they have nothing to do with React. <br>
>If you want to prevent props meant to be consumed by styled components from being passed to the underlying React node or rendered to the DOM element, you can prefix the prop name with a dollar sign ($), turning it into a transient prop.

you can learn more here you can find an example.

huangapple
  • 本文由 发表于 2023年7月6日 15:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定