[React] Radium:updating Button Styles via Props

Source: Internet
Author: User



In a CSS library like Bootstrap we can set a button's style to be "primary" or "secondary" by appending classes. For React, we want to is able to does this via props. Radium enables this by composing styles via an array. This mimicks the cascade of CSS.






Radiumn allows ' style ' attr accepts array. Inside array, the larger index style would overwrite the pervious index ' s style.


  <button style={[ styles.base,
    type===‘primary‘ && styles.primary
  ]}> {children} </button>


So in the code, Styles.primary'll overwrite the styles.base:


 
const styles = { base: {
    backgroundColor: ‘#aaa‘,
    border: ‘none‘,
    borderRadius: 4,
    color: ‘#fff‘,
    padding: ‘5px 20px‘,
    ‘:hover‘: {
      backgroundColor: ‘#08f‘ } },
  primary: { backgroundColor: ‘#07d‘ } }





We can pass a props to the component-tell when should apply Styles.primary style:


const { render } = ReactDOM
const rootEl = document.getElementById(‘root‘)

const Button = Radium(({ children, kind }) => ( <button style={[
    styles.base,
    kind === ‘primary‘ && styles.primary
  ]}> {children} </button> ))

const styles = {
  base: {
    backgroundColor: ‘#aaa‘,
    border: ‘none‘,
    borderRadius: 4,
    color: ‘#fff‘,
    padding: ‘5px 20px‘, ‘:hover‘: {
      backgroundColor: ‘#08f‘ }
  },
  primary: {
    backgroundColor: ‘#07d‘ }
}

render( <Button kind="primary"> OK </Button>,
rootEl)





[React] Radium:updating Button Styles via Props


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.