Coder Social home page Coder Social logo

Comments (5)

NilsBaumgartner1994 avatar NilsBaumgartner1994 commented on May 27, 2024

one solution is to memorize the component with the optionValue. My initial though was that i could get the same instance of the methods back?

// memoize this component
function MyButtonRowWithOptionKey ({optionKey}: {optionKey: string}) {
	const {optionValue, specificFlipOption} = useFlipOptionsWithKey(optionKey);

	return useMemo(() => {
		console.log(`Rendering MyButtonRowWithOptionKey ${optionKey}`)

		return (
			<View>
				<Text>{optionKey+": "+optionValue}</Text>
				<TouchableOpacity style={{
					backgroundColor: 'orange',
					padding: 10,
					margin: 10,
				}} onPress={specificFlipOption}>
					<Text>Flip {optionKey}</Text>
				</TouchableOpacity>
			</View>
		)
	}, [optionValue])
}

from easy-peasy.

jmyrland avatar jmyrland commented on May 27, 2024

Hey @NilsBaumgartner1994 !

I don't get it, why in this simple example the components MyButtonRowWithOptionKey are rerendering both, despite i only change a value in one?

Both components are re-rendered in both cases because both are dependent on the options object in the store.

When updateOptions is called, the options reference is updated (to the new object), which in turn triggers a rerender for all components depending on options from the store.

If you want to "fix" this in the store, you'd have to split the options into separate values in the store & update these individually.

Hope that clarifies things.

one solution is to memorize the component with the optionValue

Given that the options values are primitives, your example should prevent view-rerenders for unchanged options values.

from easy-peasy.

NilsBaumgartner1994 avatar NilsBaumgartner1994 commented on May 27, 2024

I found where the problem was:

The rerender was caused due to the option reference being updated. So as you said it triggers a rerender for all components depending on that.

If you want to "fix" this in the store, you'd have to split the options into separate values in the store & update these individually.

My solution would be this. Seems a bit easier in my optionion?

// BAD
const options = useStoreState((state) => state.options);
const optionValue = options?.[optionKey];

// Correct
const optionValue = useStoreState((state) => state.options?.[optionKey]);

from easy-peasy.

NilsBaumgartner1994 avatar NilsBaumgartner1994 commented on May 27, 2024

@jmyrland is my solution overlooking something?

from easy-peasy.

jmyrland avatar jmyrland commented on May 27, 2024

I found where the problem was:

The rerender was caused due to the option reference being updated. So as you said it triggers a rerender for all components depending on that.

If you want to "fix" this in the store, you'd have to split the options into separate values in the store & update these individually.

My solution would be this. Seems a bit easier in my optionion?

// BAD
const options = useStoreState((state) => state.options);
const optionValue = options?.[optionKey];

// Correct
const optionValue = useStoreState((state) => state.options?.[optionKey]);

I think that should work. It should only rerender when the options[optionKey]-state changes 👍

My only concern is; what happens if options?.[optionKey] resolves to undefined. If you have tested this, and it works - you should be fine 👌

from easy-peasy.

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.