javascript - Best way to create a Wizard component in React - Stack Overflow

I recently started learning React and I'm trying to create a Wizard ponent.Originally I was pictu

I recently started learning React and I'm trying to create a Wizard ponent. Originally I was picturing that it could be used like this:

<Wizard isVisible={this.state.isWizardVisible}>
    <Page1 />
    <Page2 />
</Wizard>

I started running into problems (still having an OOP mindset) when I wanted to extract properties, even ponents from the children (Page1, Page2) to configure/overwrite the Wizard (title, footer ponents, etc).

This of course can't be done, because on Wizard.render I don't have access to the ponents themselves.

Then I came across this repo with a Wizard ponent. The ponent in the repo is configured with an array of Steps, which are javascript objects to configure the current step in the wizard and contain a ponent property which returns the React ponent.

That approach certainly solves the problem but I'm wondering if it could be done differently or if there's a more "React" way of solving it, in a way that each Page/Step ponent encapsulates the behavior and not the other way around (like in the repo).

I recently started learning React and I'm trying to create a Wizard ponent. Originally I was picturing that it could be used like this:

<Wizard isVisible={this.state.isWizardVisible}>
    <Page1 />
    <Page2 />
</Wizard>

I started running into problems (still having an OOP mindset) when I wanted to extract properties, even ponents from the children (Page1, Page2) to configure/overwrite the Wizard (title, footer ponents, etc).

This of course can't be done, because on Wizard.render I don't have access to the ponents themselves.

Then I came across this repo with a Wizard ponent. The ponent in the repo is configured with an array of Steps, which are javascript objects to configure the current step in the wizard and contain a ponent property which returns the React ponent.

That approach certainly solves the problem but I'm wondering if it could be done differently or if there's a more "React" way of solving it, in a way that each Page/Step ponent encapsulates the behavior and not the other way around (like in the repo).

Share Improve this question asked Mar 18, 2016 at 21:35 user1294431user1294431 6351 gold badge7 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The "React" way of solving it is to keep your whole application state at the very top of your ponent hierarchy. Of course, React is essentially just the V in MVC, and not opinionated when it es to how you manage your data and state logic. But what you want to achieve is something like this:

<Wizard isVisible={this.state.isWizardVisible}>
    <GenericPage foo={something} bar={somethingElse} />
</Wizard>

By this logic, Wizard has access to all the application state you wish to provide, and pass down whatever GenericPage needs to render the current page. If you need a GenericPage ponent to be able to modify state, just pass down functions as props as well.

There are several frameworks that implement mon ways to handle changes in the application state. Facebook has Flux, but Redux seems to push all the right buttons and is my framework of choice.

You can try memoizing the props with:

https://github./gilbox/react-derive

then you can pass the props from Page1 to Page2 ponent.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745400024a4626057.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信