英文:
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 './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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论