How to Integrate Bootstrap with ReactJS | Pluralsight
文章推薦指數: 80 %
投票人數:10人
At this stage, the library will not work as expected since it does not include Bootstrap CSS. We'll need to add it manually as shown below:.
ChrisParkerHowtoIntegrateBootstrapwithReactJSChrisParkerJan10,202016Minread61,945ViewsJan10,202016Minread61,945ViewsWebDevelopmentReactIntroduction143IntroductionWhyBootstrapComponentsCannotBeIncludedwithReactHowtoSetupBootstrapStylesheetwithReactHowtoUseRegularBootstrapClassesWithReactUsingThird-partyLibrariesforReactandBootstrapHowtoSetUpReactstrapLibraryUnderstandingBootstrapGridUsingBootstrapComponentsWithReactConclusionTopIntroductionWe'llfocusonthefollowingskillsinthisguide:BuildingUIinaReact-basedwebappandusingBootstrap'slookandfeel.UsingreactstraptodevelopaReactappforlistingcontacts.ReactisthemostcommonlyusedJSframeworkfordevelopinginteractivewebapps.
Butsinceitisaviewlibrary,itdoesnotcomewithtechniquestobuilddesignsthatareresponsiveandveryintuitive.Inordertoovercomethis,wecanuseBootstrap,whichisafront-enddesignframework.WhyBootstrapComponentsCannotBeIncludedwithReactAddingaHTMLtagliketoanHTMLfilelikeindex.htmlisaneasytask,butit’snotthesamewhenaddingBootstraptoReact.ThereasonisthatBootstrapreliesonjQuerytorunparticularuserinterfacecomponents.AndjQuerymanipulatestheDOMdirectly,whichcontradictsthedeclarativeapproachofReact.
WecanusethesimplevanillaBootstrapstylesheetforwebapplicationsiftherequirementislimitedtoareceptive12-columngridorcomponentsthatdonotinvolvejQuery.Otherwise,therearedifferentlibrariesthatcanenabletheuseofBootstrapalongwithReact.We'llexaminebothmethodssothatwecanchoosewhichoneisbestsuitedforagivenscenario.
We'llfocusontheimplementationdetailsforintegratingBootstrapwithReactapp.HowtoSetupBootstrapStylesheetwithReactWewillbeusingCreateReactAppCLItogetstartedwithourReactproject.Itdoesnotrequireanyconfigurationtostart.ThefollowingcommandscanbeusedinstallCreateReactAppandtostarttheserverindevelopmentmode:1$create-react-appmy-bootstrap-react-app
2$cdmy-bootstrap-react-app
3$npmstart
javascriptThedirectorystructuregeneratedbyCreateReactAppisasbelow:1.
2├──package.json
3├──public
4│├──favicon.ico
5│├──index.html
6│└──manifest.json
7├──README.md
8├──src
9│├──App.css
10│├──App.js
11│├──App.test.js
12│├──index.css
13│├──index.js
14│├──logo.svg
15│└──registerServiceWorker.js
16└──yarn.lockjavascriptThenextstepistotakethelatestBootstraplibraryfromitsofficialwebsite.ThedownloadedpackageconsistsofthecompiledaswellasminimizedversionsofJavaScriptandCSSfiles.Thereisagrid-specificstylesheetaswellforappsthatjustneedtomakeuseofgrids.
ThenextstepistomakeanewfolderforCSSinpublic,copybootstrap.min.cssfilethere,andthenaddtherequiredcodeinpublic/index.htmltolinkit.1