chore: rename all &cx to cx, make clipppy happy

This commit is contained in:
Jonathan Kelley committed 2022-12-07 13:11:40 -08:00
1 parent e8133e9449
commit 4eefc3f854
85 files changed
+256 -271

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@ fn main() {
}
pub fn App(cx: Scope) -> Element {
let is_logged_in = use_state(&cx, || false);
let is_logged_in = use_state(cx, || false);
cx.render(rsx!(LogIn {
is_logged_in: **is_logged_in,
+6 -6
View File
@@ -17,13 +17,13 @@ fn App(cx: Scope) -> Element {
// But `if` statements only run if the conditional is true!
// So we might violate rule 2.
if you_are_happy && you_know_it {
let something = use_state(&cx, || "hands");
let something = use_state(cx, || "hands");
println!("clap your {something}")
}
// ✅ instead, *always* call use_state
// You can put other stuff in the conditional though
let something = use_state(&cx, || "hands");
let something = use_state(cx, || "hands");
if you_are_happy && you_know_it {
println!("clap your {something}")
}
@@ -33,12 +33,12 @@ fn App(cx: Scope) -> Element {
// ❌ don't call hooks inside closures!
// We can't guarantee that the closure, if used, will be called at the same time every time
let _a = || {
let b = use_state(&cx, || 0);
let b = use_state(cx, || 0);
b.get()
};
// ✅ instead, move hook `b` outside
let b = use_state(&cx, || 0);
let b = use_state(cx, || 0);
let _a = || b.get();
// ANCHOR_END: closure
@@ -50,12 +50,12 @@ fn App(cx: Scope) -> Element {
// ❌ Do not use hooks in loops!
// In this case, if the length of the Vec changes, we break rule 2
for _name in &names {
let is_selected = use_state(&cx, || false);
let is_selected = use_state(cx, || false);
println!("selected: {is_selected}");
}
// ✅ Instead, use a hashmap with use_ref
let selection_map = use_ref(&cx, HashMap::<&str, bool>::new);
let selection_map = use_ref(cx, HashMap::<&str, bool>::new);
for name in &names {
let is_selected = selection_map.read()[name];
+1 -1
View File
@@ -7,7 +7,7 @@ fn main() {
// ANCHOR: component
fn App(cx: Scope) -> Element {
let mut count = use_state(&cx, || 0);
let mut count = use_state(cx, || 0);
cx.render(rsx!(
h1 { "High-Five counter: {count}" }
@@ -8,8 +8,8 @@ fn main() {
// ANCHOR: component
fn App(cx: Scope) -> Element {
// ANCHOR: use_state_calls
let mut count_a = use_state(&cx, || 0);
let mut count_b = use_state(&cx, || 0);
let mut count_a = use_state(cx, || 0);
let mut count_b = use_state(cx, || 0);
// ANCHOR_END: use_state_calls
cx.render(rsx!(
+1 -1
View File
@@ -7,7 +7,7 @@ fn main() {
// ANCHOR: component
fn App(cx: Scope) -> Element {
let list = use_ref(&cx, Vec::new);
let list = use_ref(cx, Vec::new);
let list_formatted = format!("{:?}", *list.read());
cx.render(rsx!(
+1 -1
View File
@@ -7,7 +7,7 @@ fn main() {
// ANCHOR: component
fn App(cx: Scope) -> Element {
let name = use_state(&cx, || "bob".to_string());
let name = use_state(cx, || "bob".to_string());
cx.render(rsx! {
input {
+1 -1
View File
@@ -18,7 +18,7 @@ fn MemeEditor(cx: Scope) -> Element {
width: fit-content;
";
let caption = use_state(&cx, || "me waiting for my rust code to compile".to_string());
let caption = use_state(cx, || "me waiting for my rust code to compile".to_string());
cx.render(rsx! {
div {
+6 -6
View File
@@ -14,10 +14,10 @@ struct DarkMode(bool);
pub fn App(cx: Scope) -> Element {
// ANCHOR: context_provider
use_context_provider(&cx, || DarkMode(false));
use_context_provider(cx, || DarkMode(false));
// ANCHOR_END: context_provider
let is_dark_mode = use_is_dark_mode(&cx);
let is_dark_mode = use_is_dark_mode(cx);
let wrapper_style = if is_dark_mode {
r"
@@ -47,7 +47,7 @@ pub fn use_is_dark_mode(cx: &ScopeState) -> bool {
// ANCHOR: toggle
pub fn DarkModeToggle(cx: Scope) -> Element {
let dark_mode = use_context::<DarkMode>(&cx)?;
let dark_mode = use_context::<DarkMode>(cx)?;
let style = if dark_mode.read().0 {
"color:white"
@@ -71,7 +71,7 @@ pub fn DarkModeToggle(cx: Scope) -> Element {
// ANCHOR: meme_editor
fn MemeEditor(cx: Scope) -> Element {
let is_dark_mode = use_is_dark_mode(&cx);
let is_dark_mode = use_is_dark_mode(cx);
let heading_style = if is_dark_mode { "color: white" } else { "" };
let container_style = r"
@@ -82,7 +82,7 @@ fn MemeEditor(cx: Scope) -> Element {
width: fit-content;
";
let caption = use_state(&cx, || "me waiting for my rust code to compile".to_string());
let caption = use_state(cx, || "me waiting for my rust code to compile".to_string());
cx.render(rsx! {
div {
@@ -152,7 +152,7 @@ fn CaptionEditor<'a>(
caption: &'a str,
on_input: EventHandler<'a, FormData>,
) -> Element<'a> {
let is_dark_mode = use_is_dark_mode(&cx);
let is_dark_mode = use_is_dark_mode(cx);
let colors = if is_dark_mode {
r"
+3 -3
View File
@@ -14,9 +14,9 @@ struct Comment {
pub fn App(cx: Scope) -> Element {
// ANCHOR: render_list
let comment_field = use_state(&cx, String::new);
let mut next_id = use_state(&cx, || 0);
let comments = use_ref(&cx, Vec::<Comment>::new);
let comment_field = use_state(cx, String::new);
let mut next_id = use_state(cx, || 0);
let comments = use_ref(cx, Vec::<Comment>::new);
let comments_lock = comments.read();
let comments_rendered = comments_lock.iter().map(|comment| {
+5 -5
View File
@@ -8,7 +8,7 @@ fn main() {
fn App(cx: Scope) -> Element {
// ANCHOR: spawn
let logged_in = use_state(&cx, || false);
let logged_in = use_state(cx, || false);
let log_in = move |_| {
cx.spawn({
@@ -62,10 +62,10 @@ pub fn Tokio(cx: Scope) -> Element {
}
pub fn ToOwnedMacro(cx: Scope) -> Element {
let count = use_state(&cx, || 0);
let age = use_state(&cx, || 0);
let name = use_state(&cx, || 0);
let description = use_state(&cx, || 0);
let count = use_state(cx, || 0);
let age = use_state(cx, || 0);
let name = use_state(cx, || 0);
let description = use_state(cx, || 0);
let _ = || {
// ANCHOR: to_owned_macro
+2 -2
View File
@@ -14,7 +14,7 @@ struct ApiResponse {
fn App(cx: Scope) -> Element {
// ANCHOR: use_future
let future = use_future(&cx, (), |_| async move {
let future = use_future(cx, (), |_| async move {
reqwest::get("https://dog.ceo/api/breeds/image/random")
.await
.unwrap()
@@ -47,7 +47,7 @@ fn App(cx: Scope) -> Element {
#[inline_props]
fn RandomDog(cx: Scope, breed: String) -> Element {
// ANCHOR: dependency
let future = use_future(&cx, (breed,), |(breed,)| async move {
let future = use_future(cx, (breed,), |(breed,)| async move {
reqwest::get(format!("https://dog.ceo/api/breed/{breed}/images/random"))
.await
.unwrap()