admin管理员组文章数量:1426795
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 badges2 Answers
Reset to default 3The "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.
本文标签: javascriptBest way to create a Wizard component in ReactStack Overflow
版权声明:本文标题:javascript - Best way to create a Wizard component in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745400024a2656993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论