rename 'bootstrap.min.css' to 'bootstrap.min.module.css'
2022年8月2日星期二
bootstrap module css in react
2022年7月31日星期日
6 Different Ways to add CSS in React JS
Hi guys!.
In this post, we will be looking into the best ways CSS code can be added in React JS or to you React App.
Obviously, CSS is crucial in making your app user friendly and visually attractive. These are the different ways to add CSS to your react app:
Please check out my new tutorial.
1 - External Stylesheet
You can create a new CSS file in your project directory and add your CSS inside it. You can then import it in your component, class or React JS page.
The following code is used to import an external CSS stylesheet.
import "./styles.css";
2 - Inline CSS
Probably the most common and quickest out of all 3 is inline CSS. However it has many disadvantages and it is generally discouraged to use unless it is a very small application.
Basically, we create an object that contains different references which are then called using the style{} attribute.
For example, the CSS is added like this:
const styles = {
section: {
fontSize: "18px",
color: "#292b2c",
backgroundColor: "#fff",
padding: "0 20px"
},
wrapper: {
textAlign: "center",
margin: "0 auto",
marginTop: "50px"
}
}
It is then added to an element like this:
<section style={styles.section}>
<div style={styles.wrapper}>
</div>
</section>
3 - Styled Components
Probably the most powerful and useful in my opinion is Styled Components. Styled Components lets you write actual CSS in your JavaScript. The main advantage is that you can add conditional code and use variables and functions within the CSS!.
You can install Styled Components using the following command:
npm install --save styled-components
Next, you need to import it in you component. Then you can create a new variable that will contain the CSS. The same variable name with open and close brackets will render or create an HTML element with the previously added styles on it.
import styled from 'styled-components'
// Create a button variable and add CSS
const Button = styled.button`
background: transparent;
border-radius: 3px;
border: 2px solid red;
color:red;
`
//display the HTML
render(
<div>
<Button>Button</Button>
</div>
);
Other than these, there are 3 more useful ways you can add CSS (credit to lukeshiru):
4 - CSS Modules
You can also add scoped styles quite easily, you just need to create a file with the extension .module.css, like this:
// ComponentName.module.css
.Red {
color: #f00;
}
.Blue {
color: #00f;
}
Then you import it like this:
import styles from "./ComponentName.module.css";
and use it like this:
<span className={styles.Red}>This text will be red</span>
<span className={styles.Blue}>This text will be blue</span>
5 - Atomic CSS
One of the most popular atomic CSS frameworks out there is Tailwind, by just making it part of your project following their instructions you can just use classNames without even touching CSS.
<button className="font-bold bg-blue-600 px-6 py-3 text-white rounded-md">Blue button</button>
6 - Emotion
Styled-components is not the only library that allows you to create components with styles embeded on them, you have great alternatives out there such as Emotion. The best thing about Emotion is that it's framework agnostic, so you can take your knowledge to other libraries and frameworks besides React, while being pretty similar to styled-components (so you can in several scenarios just change the import).
And there you have it. I am sure there are many more out there but I think these options tick most of the boxes needed when adding CSS to your app.
Until next time.
Cheers!
2022年7月26日星期二
[PHP] split a large file to parts and merge them back to a file
//split
//merge
[PHP] log peak ram usage
//memory usage ===============================================
[PHP] stream and download large file to client (with low server ram usage)
header('Content-Disposition: attachment; filename="4k.jpg"');
2022年7月21日星期四
[reactjs] useRef
Does Not Cause Re-renders
const count = useRef(0);count.current = count.current + 1;
2022年7月19日星期二
[php] split explode string
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Laravel Eloquent Query Using WHERE with OR AND OR (orWhere)
Model::where(function ($query) use ($a,$b) {
$query->where('a', '=', $a)
->orWhere('b', '=', $b);
})
===============================if (isset($_GET["ids"])) { $ids = $_GET["ids"]; $id_list = explode(",", $ids); $notes = Note::where(function ($query) use ($id_list) { $query->where(true); foreach ($id_list as &$id) { $query->orWhere('id', $id); } }); $notes = $notes->get(); return response()->json($notes); }
Laravel change public_path()
$app->bind('path.public', function() {
return __DIR__; });
==============================
adding to public/index.php
after the line of $app = require_once __DIR__.'/../techdesignism/bootstrap/app.php';
2022年7月18日星期一
How to clear/disable js warnings console log error?
How to clear/disable js warnings console log error?
console.log = console.warn = console.error = () => {};
You can overwrite the console methods to make them no-op. Just make sure that you run this code before any other code.
2022年7月17日星期日
[react] componentwillunmount in functional component
useEffect(() => {
return () => {
// componentwillunmount in functional component.
// Anything in here is fired on component unmount.
}
}, [])
[react] Warning: Invalid DOM property `for`. Did you mean `htmlFor`?
So, your render function should be (I only replaced for with htmlFor):
[react] two component object in one { }
{[codetagtree,designtagtree] }
codetagtree is a component object in <></>
designtagtree is a component object in <></>
2022年7月16日星期六
[laravel] unique column migrate
建立索引
結構建構器支援多種類型的索引。首先,讓我們看看一個指定欄位的值必須是唯一的例子。要建立索引,我們可以簡單的在欄位定義之後鏈結上 unique 方法:
$table->string('email')->unique();此外,你可以在定義完欄位之後建立索引。例如:
$table->unique('email');
[Fixed] some windows chrome, scrolling has glitch position not accurate
[Fixed] some windows chrome, scrolling has glitch position not accurate Smooth Scrolling Flag 📜 Chrome has an experimental "smooth ...
-
[Fixed] some windows chrome, scrolling has glitch position not accurate Smooth Scrolling Flag 📜 Chrome has an experimental "smooth ...