Comments (17)
There is another problem, but I am not sure about the cause and whether it is a bug
- Render [VNode 1, VNode2]
- Update props and render [VNode 1]
render
of VNode2 will run
from preact.
@foray1010 I notice you're on 3.4.0
- is this happening on preact 3.4.0
? There is a fix for keys in that release:
from preact.
Yes, I am in 3.4.0
from preact.
Regarding your second issue, that's just how react/preact works. If you are building components that are deterministic in their behaviour when re-rendered for the same props, I would suggest using a shouldComponentUpdate
hook or @pure
decorator:
class Foo extends Component {
shouldComponentUpdate(props) {
for (let i in props) if (props[i]!==this.props[i]) return true;
return false;
}
}
or the decorator approach:
// only re-render when props *change*:
@pure
class Foo extends Component {
render() { }
}
// here's an example implementation of that decorator:
function pure(ref) {
ref.prototype.shouldComponentUpdate = function(props) {
for (let i in props) if (props[i]!==this.props[i]) return true;
return false;
};
}
from preact.
I think my case is a bit different:
I have a state a
which is an array [{text: '1'}, {text: '2'}]
inside component b
, I render an array of component c
depends on state a
const b = (props) => {
// a is connected by redux
let items = props.a.map((value, index) => (<c index={index} />))
return <div>{items}</div>
}
const c = (props) => (
<div>{props.a[props.index].text}</div>
)
So, after I set state.a
to [{text: '1'}]
only, the c
that was related to value {text: '2'}
will fail to render because props.a[props.index]
is undefined
I thought when rerender, preact should know that in b
, I don't have c
with index 1
anymore, so why need to run render
again?
from preact.
@foray1010 In your example, you're not passing the a
prop to <C>
.
Here is an example that seems to show what you describe, but it does not incorrectly re-render <C>
:
https://jsfiddle.net/developit/va7benuj/
from preact.
@developit thanks for the example! I may not be so clear, I should said that a
is connected to all components by redux
Maybe it only happen when use with redux, I will generate an example to u tmr
from preact.
Alright, let me know. It seems like the issue would be that the child component (C
) is being re-rendered prior to the parent, so the parent has not yet removed it.
from preact.
I try to minimize the case, here it is: https://github.com/foray1010/test-preact , it seems only happen when use with redux
Main logic is in here: https://github.com/foray1010/test-preact/blob/master/js/containers/App.jsx
As I don't know how to use jsfiddle.net, please git clone it and
- npm install
- npm install -g gulp
- gulp dev
- run __dev/test.html
The error Uncaught TypeError: Cannot read property 'text' of undefined
occur after you click 1
and 2
Update: if no key for Item
, every time you click will have error
from preact.
Thanks for the awesome example, looking into this now.
from preact.
Fixed on master, releasing as 4.0
shortly.
from preact.
4.0
doesn't fix the example: https://github.com/foray1010/test-preact
I still get Uncaught TypeError: Cannot read property 'text' of undefined
from preact.
The issue is that you're mapping parent state to the same prop. Redux is triggering a re-render of your child component (<Item>
), but only after the props have already triggered a re-render. A more typical approach to this would be to instead pass the item itself as a prop, rather than relying on <item>
to obtain it from redux.
Can you try setting syncComponentUpdates=false
? Or I can.
import { options } from 'preact';
options.syncComponentUpdates = false;
I'm curious if it would fix the issue.
from preact.
no it doesn't fix the issue
from preact.
The issue in comment #1 is fixed by updating preact-compat from 0.7.1 to 1.6.0
from preact.
Sweet! The other issue still happens though?
from preact.
yes, I hv just updated https://github.com/foray1010/test-preact
Issue still happen no matter options.syncComponentUpdates is true or false
from preact.
Related Issues (20)
- Aliasing React to Preact in Deno HOT 9
- "Cannot read properties of null (reading '__H')" when hydrating a component in the browser HOT 1
- (question)Is there any practical example of purely using CDN? HOT 5
- TypeError: undefined is not an object (evaluating 'this.l[e.type+!1]') HOT 1
- preact+react-modal,Uncaught TypeError: Cannot read properties of null (reading '__H') HOT 9
- Island architecture with partial hydration HOT 3
- preact renders only one iframe if more than one iframe provided to dangerouslySetInnerHTML HOT 2
- When the value of setState remains unchanged, the state-driven controlled component, such as <input/>, seems to no longer be under the influence of the state. HOT 1
- There is no `context` in `preact/compat/PureComponent` and it breaks other libraries like `react-recurly`
- Running the text.test.js test, the results obtained are very poor and differ many times from the expected values in the code. HOT 3
- Components keep being added when updating state HOT 1
- Add JSX TS aria roles for dpub-aria-1.0
- `TypeError: Cannot set properties of null (setting '__m')` with preact 10.19.4 when destroying a component that calls `useId`
- Problem with rendering child component on props changes HOT 3
- "Components keep being added when updating state" issue persists when using tags other than div HOT 1
- preact/compat's SetStateAction is different from React's SetStateAction HOT 4
- useSelector hook stops being called under certain conditions since preact 10.19.4 HOT 3
- Implement onscrollend event attribute for TypeScript JSX
- TypeScript JSX focus events don't have consistent case HOT 5
- Typescript issues in 10.19.4+ with @mui/material HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from preact.