Code Monkey home page Code Monkey logo

Comments (3)

MaxTwentythree avatar MaxTwentythree commented on May 25, 2024 1

Thanks for your quick answer!
It does make sense and made it clearer to me!
The issue can be closed.
Thank you :)

from react-rxjs.

MaxTwentythree avatar MaxTwentythree commented on May 25, 2024

Oooh, of course right after creating this issue, I had the idea that the mutability of the map might be the problem.

So with this:

  scan((map, b) => {
    return {...map, [String(b)]: b};
  }, {})),

It works!
It kinda makes sense..

I'm not super happy creating a new map on every event though .. is there another way, or am I approaching this completely wrong?

from react-rxjs.

voliva avatar voliva commented on May 25, 2024

Yes, this is not something that React-RxJS does, but it comes from React itself.

Try this sandbox -> https://stackblitz.com/edit/react-ts-dwqy2x?file=App.tsx,index.tsx

export default function App() {
  const [values, setValues] = useState([]);

  return (
    <div>
      <button
        onClick={() =>
          setValues((v) => {
            v.push(Math.random());
            return v;
          })
        }
      >
        Add
      </button>
      {values.map((v, i) => (
        <div key={i}>{v}</div>
      ))}
    </div>
  );
}

If you click Add, nothing really happens. The values are being added to the array, but React detects it's the same array referentially, so it won't update the component. If you recreate a new array inside setValues, then it will start working.

The observables do create a new emission, so you can still use them to compose other state (as long as you keep it in mind... Operators like distinctUntilChanged, pairwise, etc. won't work as expected). Something I sometimes do is only recreate the map/set/array at the very bottom of the state tree, right before using it in the react components.

from react-rxjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.