javascript - styled component how to pass width props to component - Stack Overflow

I'm new in styled ponents . I want to set input width by passing props to ponent. Expected output

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
Add a ment  | 

1 Answer 1

Reset to default 5

In 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信