I'm new in styled ponents . I want to set input width by passing props to ponent. Expected output should be :
<Input width=30px> or <Input width=100%>
Here is my ponent:
import styled from "styled-ponents"
const UIInput = styled.input`
padding: 5px;
width: ${props => props.width ? width : 'auto'}
`
export default UIInput
I'm new in styled ponents . I want to set input width by passing props to ponent. Expected output should be :
<Input width=30px> or <Input width=100%>
Here is my ponent:
import styled from "styled-ponents"
const UIInput = styled.input`
padding: 5px;
width: ${props => props.width ? width : 'auto'}
`
export default UIInput
Share
Improve this question
asked May 6, 2019 at 9:18
Yerlan YeszhanovYerlan Yeszhanov
2,44913 gold badges42 silver badges78 bronze badges
1 Answer
Reset to default 5In this little snippet: ${props => props.width ? width : 'auto'}
, you forget that width
is on the props
-object. To fix it:
${props => props.width ? props.width : 'auto'}
You can see my working example below:
const UIInput = styled.input`
padding: 5px;
width: ${props => props.width ? props.width : 'auto'}
`
ReactDOM.render(
<div>
<div>
<p>300px</p>
<UIInput width="300px" />
</div>
<div>
<p>80%</p>
<UIInput width="80%"/>
</div>
</div>,
document.getElementById('root'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://unpkg./styled-ponents/dist/styled-ponents.min.js"></script>
<div id="root"></div>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744293384a4567140.html
评论列表(0条)