Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3878

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:48:09+00:00 2024-11-26T06:48:09+00:00

Tips for styling React apps in JavaScript

  • 61k

 react and Javascript

While many developers still use CSS to style their React apps, writing styles in JavaScript has become an increasingly popular practice as it gives all of the conveniences of CSS preprocessors without the need to learn a new language.

Most CSS-in-JS libraries involve creating a JavaScript object of styles. As an example, this is what a style object looks like using my preferred library, Aphrodite.

const styles = StyleSheet.create({   container: {     position: 'relative',     width: 40,     height: 50,   }, )} 
Enter fullscreen mode Exit fullscreen mode

Variables

Many best practices from SASS/LESS workflows still apply when using JavaScript styles. Colors fonts, and other constants can be stored as variables, which can be used like so:

// colors const YELLOW = '#FFF83C'; // fonts const BOLD = 700; const TIMES_NEW_ROMAN = '"Times New Roman", Georgia, Serif'; // shared constants const border = `1px solid ${YELLOW}`; const styles = StyleSheet.create({   textBox: {     color: YELLOW,     fontFamily: TIMES_NEW_ROMAN,     fontWeight: BOLD,     border,    }, )} 
Enter fullscreen mode Exit fullscreen mode

New ES6 language features come are handy for sharing styles cleanly. In the above example, border is referenced with the object literal shorthand notation.

Use JS objects in place of mixins

The spread operator (…) is particularly useful when defining a commonly-used sequence of styles (in this example the rules needed to add an ellipsis to overflowing text).

const textEllipsis = {   whiteSpace: 'nowrap',   textOverflow: 'ellipsis',   overflow: 'hidden',   width: '100%', }; const styles = StyleSheet.create({   textBox: {     ...textEllipsis,     backgroundColor: 'gray',   }, )} 
Enter fullscreen mode Exit fullscreen mode

Create helper functions for more complex motifs

More complicated sequences of rules can be abstracted behind helper functions. For example, when styling placeholder text, I use a function that returns an object of styles:

const getPlaceholderStyles = obj => ({   '::-webkit-input-placeholder': obj,   '::-moz-placeholder': obj,   ':-ms-input-placeholder': obj,   ':-moz-placeholder': obj,   ':placeholder': obj, }); const styles = StyleSheet.create({   input: getPlaceholderStyles({ color: RED }), )} 
Enter fullscreen mode Exit fullscreen mode

Be consistent about naming schemes

One of biggest advantages of writing styles in JavaScript is that classNames are scoped to each component by default. This means that conventions like BEM are unnecessary. Still, it is helpful to be consistent with a naming scheme for classNames. For example, I use container for the outermost className of each React component.

If you want to support my channel and see more great content, hit that subscribe button.😍

Welcome to Learn with Daniel I'm Danial and on this channel, you'll find tutorials on HTML, CSS, and JavaScript, as well as creative UI/UX designs, CSS animations, and effects. Whether you're a beginner or an experienced developer, I hope you find my content helpful and enjoyable. My goal is to inspire and empower design-focused frontend developers & designers. Don't forget to subscribe to my channel and enable notifications by clicking the bell icon. That way, you'll be the first to know about new tutorials and resources as soon as they're released. #html #javascript #css #coding #frondend #htmlcss #javascriptprojects #programming #css #trend #viral #program #shortsvideo #shorts #youtubeshort

favicon youtube.com

javascriptprogrammingreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.