I am trying to create a component, whose width can be specified wherever the component can be used
Like:
<Button label="button" width="80%" />
const TestButton = styled.button`
color: red;
`;
var React = require('react');
var Button = React.createClass({
render: function () {
return (
<TestButton>{this.props.label}</TestButton>
);
}
});
module.exports = Button;
how can I achieve this?
You can pass width
as props to button component like,
export const Button = (props) => { // Your button component in somewhere
return (
<button style={{width: `${props.width}`}}>{props.label}</button>
)
}
In your main component import
your button and work as what you want like below
import Button from 'your_button_component_path';
class RenderButton extends React.Component {
render() {
return (
<Button width="80%" label="Save" />
);
}
}
If you're using styled-components
you can pass the width prop to the component and set its width:
<Button label="button" width="80%" />
const TestButton = styled.button`
color: red;
width: ${(props) => props.width}
`;
var React = require('react');
var Button = React.createClass({
render: function () {
return (
<TestButton width={this.props.width}>{this.props.label}</TestButton>
);
}
});
module.exports = Button;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With