useObserver vs @observer with mobx-react 6.3.1 #2713
Answered
by
danielkcz
vladimir-djokic
asked this question in
Q&A
-
I have a head scratcher, so if anyone could enlighten me as to what I'm doing wrong. I'm using:
Consider following code: import { observable, transaction } from "mobx";
import { observer, useObserver } from "mobx-react";
import * as React from "react";
import "./styles.css";
interface Info {
id: number;
}
// @observer
// export class Row extends React.Component<Info> {
// render(): React.ReactNode {
// console.log(`Row.render(${this.props.id})`);
// return (
// <tr>
// <td>{this.props.id}</td>
// <td>foo</td>
// </tr>
// );
// }
// }
export const Row: React.FunctionComponent<Info> = (props) => {
return useObserver(() => {
console.log(`Row.render(${props.id})`);
return (
<tr>
<td>{props.id}</td>
<td>foo</td>
</tr>
);
});
};
@observer
export class List extends React.Component {
render(): React.ReactNode {
console.log("List.render()");
return (
<div>
<table>
<tbody>
{this.rows.map((o) => (
<Row key={o.id} id={o.id} />
))}
</tbody>
</table>
<button onClick={this.handleClick}>Load More...</button>
</div>
);
}
readonly handleClick = () => {
transaction(() => {
for (let i = 0; i < 5; ++i) this.rows.push({ id: this.rows.length });
});
};
@observable rows: Info[] = [];
}
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<List />
</div>
);
} Running this using either
So, the questions are:
|
Beta Was this translation helpful? Give feedback.
Answered by
danielkcz
Jan 13, 2021
Replies: 1 comment 3 replies
-
Please provide a runnable example (with eg. CodeSandbox) so we can see it in action. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
vladimir-djokic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please provide a runnable example (with eg. CodeSandbox) so we can see it in action.