Closed stavenko closed 8 years ago
After a whole day of looking for mistery thing in my code, I have discovered the main reason of this bug:
In some undefined moment, one nasty chrome extension prepended two div
elements and appended one element to container, from which react should have removed the staff. (Those my english madskills).
Disabling this extention solved my problem and made my day completed. No more work on this particular problem is required, but, I'd rather suggested to all you, respectful developers to implement some small functionality, that under the hood, filters non-react elements out to hell. Or tells to some noob like me, that page state changes in undefined way.
Yeah, this is something we are considering. You can help, too: https://github.com/facebook/react/issues/3218
On an unrelated note, I some anti-patterns in your code. While this not relevant to this issue I figure I’d mention them:
1) Deriving state based on props is usually an anti-pattern. It’s better to move state management to the parent component and provide a callback to the child as a prop.
2) Using forceUpdate()
is also an anti-pattern. React will automatically re-render your component when you call setState()
in it or its parent.
3) Assinging something directly to anything inside this.state
is an anti-pattern. I suggest you to create a new object based on the old one, and call setState()
to update the relevant part of the state with a reference to the new object. Treating state as immutable lets you add powerful performance optimizations later because it is possible to compare which objects have changed by comparing their references with the previous values.
4) There is never a reason to assign this.props
. Props are immutable. React will update the reference to this.props
after componentWillReceiveProps()
exits. Don’t do this manually.
I recommend you to read through Thinking in React which covers top-down data flow and state ownership. While React provides escape hatches that you are using, they are absolutely not the primary way to build React apps, and you might have problems later on unless you got back to these topics and get familiar with them.
I hope that this is helpful. Cheers!
3) Assinging something directly to anything inside this.state is an anti-pattern.
I would really appreciate some advices to achieve this with a single this.setState
instruction. I had an issue with complex forms, when I had to set properties with objectPath.set(this.state, 'some.sophisticated.input.data.path', e.currentTarget.value)
; I had to to this with let s = this.state; <modify s>; this.setState(s)
, which seems too me same as <modify this.state>; this.setState(this.state)
. Actually, I started to use <modify this.state>; this.forceUpdate()
;
4) There is never a reason to assign this.props. Whoa. Since I have debug-ed react a lot to find reason of this issue, now I see abilities how too prevent state where it's not needed and immutable props, this knowledge will affect a lot of my previously done code.
You can use something like React immutability helpers for working plain objects or Immutable.js which provides API for deep updates.
In general if your state is very complicated you might want to consider moving its handling out of the component with a library like http://redux.js.org (disclaimer: I wrote it). It lets you describe changes in state as pure functions that call each other, and “reducer composition” pattern used there corresponds to the deep updates you want to implement.
Finally, even if you mutate the state directly, you still don’t need to reassign the props. The correct way to go with mutation is to perform it in the component that owns the state, and to call setState
in it. Then the change will propagate down to any children—you don’t need to do anything with props to make it happen.
Hello.
I got one strange error-like thing. I got array of commands in very special component, eg:
Each command is a:
And
CurrentCommand
is a:or
There is a remove button in command root. The main idea is to remove certain element in array and then redraw it. Sometimes i can see, that array is spliced correctly - it identity stored correctly. But sometimes, wrong element is deleted with exception:
There's explanation in this exception:
I have no any of this elements, I have putted keys to commands, and it seems to me, that react doesn't handle elements with
key
property correctly.When i try too look at available elements, it was exists before
remove
function called.I have no idea, why incorrect dom element is removed(since removed element left on page), and when I have correct array when i
console.log
it's items inScenarioForm::render
method.