Giter Site home page Giter Site logo

replace object not trigger render about valtio HOT 3 CLOSED

lake2 avatar lake2 commented on June 9, 2024
replace object not trigger render

from valtio.

Comments (3)

dai-shi avatar dai-shi commented on June 9, 2024
const snap = useSnapshot(it.obj);
// that โ˜๏ธ means like this ๐Ÿ‘‡ 
const objThatNeverChanges = it.obj;
const snap = useSnapshot(objThatNeverChanges);

So, it's how JS works. The canonical fix is to subscribe to obj itself.

const { obj: snap } = useSnapshot(it);

from valtio.

lake2 avatar lake2 commented on June 9, 2024
const snap = useSnapshot(it.obj);
// that โ˜๏ธ means like this ๐Ÿ‘‡ 
const objThatNeverChanges = it.obj;
const snap = useSnapshot(objThatNeverChanges);

So, it's how JS works. The canonical fix is to subscribe to obj itself.

const { obj: snap } = useSnapshot(it);

Can I do something like replace the value to fix this?
Maybe I can make a PR to implement this replacement function and include it in valtio if this is reasonable;

const it = proxy({
    obj: { a: 1 },
});

// just a simple implementation, it may be not work in some situation
const replace = (it, key, value) => {
    Object.keys(value).forEach(item => {
        it[key][item] = value[item];
    });
};

setTimeout(() => {
    replace(it, 'obj', { a: 2 });
}, 1000);

const A: React.FC = React.memo(function A(props) {
    const snap = useSnapshot(it.obj);
    console.log('===========update', snap); // only render once

    return null;
});

from valtio.

dai-shi avatar dai-shi commented on June 9, 2024

It's a reasonable practice but you don't need to make a PR for that.
There are many other libraries that do similar things.
Feel free to submit a PR to improve docs.

from valtio.

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.