最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I define model classes in React? - Stack Overflow

programmeradmin1浏览0评论

React uses Flux architecture and it is said in .html that React has two models - state and props. And there are some suggestions for model management in React .html - but all of this seems to some additional layers to enhance the Flux. The big questions to which I am seeking the answers are:

  • Should I define model classes in React? I.e. if I have Customer class notion, then I can: 1) define the attributes of Customer directly as the attributes of state/props 2) define the attributes of Customer as the attributes of state.customer/props.customer; 3) define some JavaScript template/class Customer separately and simply say, that state.customer/props.customer is of type Customer and don't repeat attributes in the state/props. I feel, that 3) is the right approach, isn't it?
  • If 3rd options (of the previous point) is the right approach, then how can I define the Customer template and how can I define that state.customer/props.customer is of this template? I could use those template in some serialization, some model validation tasks as well and I could use in ReactNative project as well.

React uses Flux architecture and it is said in https://reactjs/docs/thinking-in-react.html that React has two models - state and props. And there are some suggestions for model management in React https://reactjs/munity/model-management.html - but all of this seems to some additional layers to enhance the Flux. The big questions to which I am seeking the answers are:

  • Should I define model classes in React? I.e. if I have Customer class notion, then I can: 1) define the attributes of Customer directly as the attributes of state/props 2) define the attributes of Customer as the attributes of state.customer/props.customer; 3) define some JavaScript template/class Customer separately and simply say, that state.customer/props.customer is of type Customer and don't repeat attributes in the state/props. I feel, that 3) is the right approach, isn't it?
  • If 3rd options (of the previous point) is the right approach, then how can I define the Customer template and how can I define that state.customer/props.customer is of this template? I could use those template in some serialization, some model validation tasks as well and I could use in ReactNative project as well.
Share Improve this question edited Jun 2, 2023 at 3:20 ggorlen 57.4k8 gold badges111 silver badges154 bronze badges asked Jun 16, 2019 at 21:34 TomRTomR 3,0766 gold badges42 silver badges108 bronze badges 13
  • 5 There are no rules for this, React purely focuses on UI - how you manage your app state is entirely up to you. – James Commented Jun 16, 2019 at 21:42
  • 2 If you dive in and start making something, you'll get a feel for how your data should be structured for your project. – jmargolisvt Commented Jun 16, 2019 at 21:47
  • 1 @James, can you point me to some examples? React does not seem to focus purely on UI. If it did it it's docs and examples wouldn't all show storing the model in react state. – user128511 Commented Oct 6, 2020 at 2:52
  • 1 @gman whilst React has mechanisms for holding state relative to a view, it is not strongly opinonated on how you manage your app state. It's a UI framework at the end of the day, and there are a few out there, so if you chose to couple your app state by holding it in views or using Context then that's a design decision that you make, and not one React forces on you. – James Commented Oct 6, 2020 at 22:49
  • 1 @James, all of the React docs use React's state, context, etc. If they were separate concerns then they'd be separate libraries. A UI library, and a separate state library. Clearly React does not focus purely on UI. If it did the state parts would not exist. Examples of how to separate the two would be way more useful the ambiguous statements that "it's possible" – user128511 Commented Oct 7, 2020 at 4:38
 |  Show 8 more ments

1 Answer 1

Reset to default 3

The most basic way is shown in following snippet:

const Customer = ({ name, age }) => (
  <div>
    <p>Name: {name}</p>
    <p>Age: {age}</p>
  </div>
);

const App = () =>
  [{ name: "Bert", age: 22 }, { name: "Alfons", age: 45 }].map(
    ({ name, age }, i) => (
      <>
        <Customer key={i} name={name} age={age} />
        <hr />
      </>
    )
  );

Where you define these props depends on where you need them. If only one ponent needs the props, you define them in that ponents state. But often you need the props in several ponents, so you lift them in your hierarchy up. That often results in a very "smart" ponent (a ponent that has a large state).

If your app bees large and confusing, I suggest you store your state externally. To do that, you can use react context. It allows you to inject props to ponents that need it, rather than passing it several layers down in your hierarchy.

If you don't want to write your own context, you may use state management solutions like redux or mobx. They use context too, but provide convenient functions to easily connect a ponent to your external state.

发布评论

评论列表(0)

  1. 暂无评论