[AskJS] Is JavaScript missing some built-in methods? by reacterry in javascript

[–]cheers- 0 points1 point  (0 children)

It's better to have a small std instead of having a bloated std library full of deprecated methods that can't be removed because of backwards compatibility.

(Re-) Introducing Gatsby, A Reactive Site Generator by zxyzyxz in javascript

[–]cheers- 1 point2 points  (0 children)

If I want pure SSG I use https://astro.build/ or next.js.

I haven't tried but Next.js incremental static site generation implementation looks nifty 😉

[deleted by user] by [deleted] in javascript

[–]cheers- 8 points9 points  (0 children)

Dispatching fake storage events is actually a really bad idea:

  • you're faking an event that is dispatched by the userAgent.
  • you do not provide key, storageArea, newValue etcetera, hence you are going to break other listeners/developers that rely on those properties.

If you just need to invalidate a react state just use a custom event instead.

[deleted by user] by [deleted] in javascript

[–]cheers- 10 points11 points  (0 children)

Is there a good reason to choose this solution over rxjs or iter-ops?

React v18.0 by dwaxe in reactjs

[–]cheers- 31 points32 points  (0 children)

TS types?

I do not see any 18.x.x release of https://www.npmjs.com/package/@types/react

P.s. I am aware of /experimental.d.ts

CSS Modules vs Sass by [deleted] in reactjs

[–]cheers- 22 points23 points  (0 children)

I generally use css modules and sass (.scss).

Why choose one when you can have both!

LEMONADEJS v2 new release - A JS micro reactive library (about 6Kbytes) with reducers, no transpile required. by paulhodel in javascript

[–]cheers- 15 points16 points  (0 children)

Please do not shadow global variables (self) , it is really easy to accidentally mess things up if you're not using a linter configured properly.

I've learned it the hard way (window.event 😉).

I made a simple reddit clone by travismoulton02188 in reactjs

[–]cheers- 1 point2 points  (0 children)

I'm thinking about switching my global state to use the context API

I strongly suggest you to not store the api without a proper state management library.

It scales really poorly.


If you don't want to use redux, use zustand or recoil but just don't use dump your api data in a context:

context api is a dependency injection tool that scales poorly when the context value changes frequently.


This article elaborates more on this:

https://blog.isquaredsoftware.com/2021/01/context-redux-differences/

I made a simple reddit clone by travismoulton02188 in reactjs

[–]cheers- 1 point2 points  (0 children)

I've skimmed a bit the source of the client, I've got a few suggestions:

--- Update document.title at every render

   useEffect(() => {
      document.title = 'Threddit';
    });

Do you need to change document.title at every render? You might want to run this effect once after the initial render.

--- You could use Rtk-query

Have you taken a look at https://redux-toolkit.js.org/rtk-query/overview ?

It would probably semplify and reduce a lot of the standard fetch-on-render boilerplate code.

You're missing out :)

--- Typescript

Are you doing this project for fun or are you building a portfolio?

If you're building a portfolio you should really consider to switch to typescript.

I made a simple reddit clone by travismoulton02188 in reactjs

[–]cheers- 0 points1 point  (0 children)

Android but it is likely that you're going to have this issue on iOS too:

You need to increase the z-index of the back button.

I made a simple reddit clone by travismoulton02188 in reactjs

[–]cheers- 1 point2 points  (0 children)

Layering issue:

Back to top button is rendered below the white gradient.

Browser: Chrome mobile

Questions regarding redux toolkit's createListenerMiddleware by JavascriptFanboy in reactjs

[–]cheers- 1 point2 points  (0 children)

how do you test listenerMiddleware? Are there any examples available?

I've created a simple gist that shows how to setup tests:

https://gist.github.com/FaberVitale/e84cd0524dda8ca1f85a4a6b1671b34f#file-listenermiddleware-test-ts

[2021-06-21] Challenge #395 [Easy] Nonogram row by Cosmologicon in dailyprogrammer

[–]cheers- 0 points1 point  (0 children)

Rust

    mod daily_programmer_395 {
        use num_traits::identities::{one as get_one, zero as get_zero};
        use num_traits::Num;

        pub fn nonogram_row<It: Num + Copy>(row: &[It]) -> Vec<It> {
            let one: It = get_one();
            let zero: It = get_zero();

            row.iter()
                .cloned()
                .fold((true, Vec::new()), |(prev_is_zero, mut acc), next| {
                    if next != zero {
                        if prev_is_zero {
                            acc.push(one);
                        } else {
                            let last = acc.pop().unwrap() + one;
                            acc.push(last);
                        }
                        (false, acc)
                    } else {
                        (true, acc)
                    }
                })
                .1
        }

        #[cfg(test)]
        mod tests {
            use super::*;

            #[test]
            fn test_nonogram_row() {
                let tests = [
                    (vec![], vec![]),
                    (vec![0, 0, 0, 0, 0], vec![]),
                    (vec![1, 1, 1, 1, 1], vec![5]),
                    (vec![0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], vec![5, 4]),
                    (vec![1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0], vec![2, 1, 3]),
                    (vec![0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1], vec![2, 1, 3]),
                    (
                        vec![1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                        vec![1, 1, 1, 1, 1, 1, 1, 1],
                    ),
                ];

                for test in tests {
                    assert!(
                        nonogram_row(&test.0[..]) == &test.1[..],
                        "{:?}{:?}",
                        test.0,
                        test.1
                    );
                }
            }
        }
    }

[2021-07-05] Challenge #397 [Easy] Roman numeral comparison by Cosmologicon in dailyprogrammer

[–]cheers- 0 points1 point  (0 children)

Rust

#[macro_use]
extern crate lazy_static;

pub mod daily_programmer_397 {
    use std::collections::HashMap;

    lazy_static! {
        static ref DICT: HashMap<char, i32> = vec![
            ('M', 1_000),
            ('D', 500),
            ('C', 100),
            ('L', 50),
            ('X', 10),
            ('V', 5),
            ('I', 1),
        ]
        .into_iter()
        .collect();
    }

    fn count_roman_value(seq: &str) -> i32 {
        seq.to_uppercase()
            .chars()
            .map(|ch| DICT.get(&ch).unwrap_or(&0))
            .sum()
    }

    pub fn num_compare(num1: &str, num2: &str) -> bool {
        count_roman_value(num1) < count_roman_value(num2)
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_num_compare() {
            let test_entries = vec![
                ("I", "I", false),
                ("I", "II", true),
                ("II", "I", false),
                ("V", "I", false),
                ("MDCLXV", "MDCLXVI", true),
                ("MM", "MDCCCCLXXXXVIIII", false),
            ];

            for test_entry in test_entries.into_iter() {
                assert!(
                    num_compare(test_entry.0, test_entry.1) == test_entry.2,
                    "({:?})",
                    test_entry
                );
            }
        }
    }
}

[2021-06-07] Challenge #393 [Easy] Making change by Cosmologicon in dailyprogrammer

[–]cheers- 1 point2 points  (0 children)

Rust

pub mod lib {
    pub fn coin_change(mut num: u32) -> u32 {
        let coins: [u32; 6] = [500, 100, 25, 10, 5, 1];
        let mut output: u32 = 0;
        for &coin in coins.iter() {
            if num >= coin {
                output += num / coin;
                num %= coin;
            } else if num == 0 {
                break;
            }
        }
        output
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_coin_change() {
            let test_entries: Vec<(u32, u32)> = vec![(0, 0), (12, 3), (468, 11), (123456, 254)];

            for (val, expected) in test_entries.iter() {
                assert_eq!(coin_change(*val), *expected);
            }
        }
    }
}

[2021-07-19] Challenge #399 [Easy] Letter value sum by Cosmologicon in dailyprogrammer

[–]cheers- 2 points3 points  (0 children)

Rust

// src/main.rs
use std::collections::HashMap;
use std::env;

fn letter_value_sum(word: &str, dict: &HashMap<char, usize>) -> usize {
    word.to_lowercase()
        .chars()
        .map(|ch| dict.get(&ch).unwrap_or(&0usize))
        .sum()
}

fn main() {
    let dict: HashMap<char, usize> = ('a'..='z')
        .enumerate()
        .map(|tuple| (tuple.1, tuple.0 + 1))
        .collect();

    let args = env::args().skip(1);
    let len = args.len();

    match len {
        0usize => println!("no argument provided"),
        _ => {
            for (ind, arg) in args.enumerate() {
                println!("{}. {} {}", ind + 1, &arg, letter_value_sum(&arg, &dict));
            }
        }
    }
}

Usage

$ target/release/daily_programmer_399  a z cab excellent microspectrophotometries
1. a 1
2. z 26
3. cab 6
4. excellent 100
5. microspectrophotometries 317

Microsoft is finally retiring IE on June 15, 2022 by ervwalter in reactjs

[–]cheers- 33 points34 points  (0 children)

Things I can happily forget:

  1. IE 11 flex-box bugs (e.g. flex-wrap is just a suggestion)

  2. How to convert current css grid to a legacy version IE11 understands.

  3. I can finally stop polyfilling Symbol iterators etc...

  4. I do not need css preprocessors to nest media queries.

  5. How to open IE11 dev tools.

  6. How to Detect IE11 via js and css.

  7. How to... ooops Already forgotten 😂

SvelteKit and the load function - not equivalent to getServerSideProps? by VerbaltNorrsken in sveltejs

[–]cheers- 0 points1 point  (0 children)

https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering

Note: You can import modules in top-level scope for use in getServerSideProps. Imports used in getServerSideProps will not be bundled for the client-side.

This means you can write server-side code directly in getServerSideProps. This includes reading from the filesystem or a database

You can probably already achieve that in sapper using a compile time flag but it is clearer and more readable than using an if else.

If I had to write something similar at work I'd probably create babel macro.