reactjs - How to implement a base component that preprocess all urls and let all other components extends it? -
when opens url domain, want keep track of query string variable(named var), idea make parent component @ componentwillmount, take query string, , save redux state variable, if state variable undefined.
for example, if comes url:
mydomain.com/anyurl/?var=abc
now set state variable abc. , when viewer navigates page within domain, var in query string can dismissed, still keep track of activity, go mydomain.com/anyurl2 still know 1 var=abc.
here approach:
import react, { component } 'react' import { recordrefurl } '../actions/actions' import { connect } 'react-redux' class basecontainer extends component { componentwillmount() { if (this.props.location.query.var && !this.props.urlrefparam) { this.props.dispatch(recordrefurl(this.props.location.query.var )); } } } function select(state) { return { urlrefparam: state.urlrefparam, } } export default connect(select)(basecontainer)
and in components(in same root folder basecontainer), let them extends basecontainer instead of component:
import react, { component } 'react' import basecontainer './basecontainer' import { connect } 'react-redux' export default class homepagehandler extends basecontainer { render() { return ( <div>hello</div> ) } }
the app.js have like:
import react 'react' import { render } 'react-dom' import { route, router, userouterhistory, hashhistory } 'react-router' import createbrowserhistory 'history/lib/createbrowserhistory' //redux stuff import { provider } 'react-redux' import { applymiddleware, compose } 'redux' import thunk 'redux-thunk' import { createstore, renderdevtools } './utils/devtools' let apphistory = apphistory = userouterhistory(createbrowserhistory)({basename: '/'}) let store = createstore(citysearchapp, compose( applymiddleware(thunk), window.devtoolsextension ? window.devtoolsextension() : f => f ) ) let routes = ( <muithemeprovider> <div classname="app"> <provider store={store}> <router history={apphistory} onupdate={firetracking}> <route name="main" component={apphandler}> <route name="home" path="/" component={homepagehandler}/> </route> </router> </provider> {renderdevtools(store)} </div> </muithemeprovider> ) render(routes, document.body)
and package.json looks like:
"dependencies": { "bluebird": "^3.4.0", "body-parser": "^1.15.1", "chai": "^3.5.0", "cors": "^2.7.1", "express": "^4.13.4", "jquery": "^2.2.4", "material-ui": "^0.19.0", "mocha": "^2.5.3", "morgan": "^1.7.0", "react": "^15.6.1", "react-addons-css-transition-group": "^15.0.0", "react-dnd": "^2.1.4", "react-dnd-html5-backend": "^2.1.2", "react-dnd-touch-backend": "^0.3.2", "react-dom": "^15.0.0", "react-flip-move": "^2.4.1", "react-ga": "2.1.2", "react-input-range": "^1.0.2", "react-mount": "^0.1.3", "react-redux": "^4.1.2", "react-router": "^2.8.1", "react-tools": "^0.13.3", "redux": "^3.1.4", "redux-thunk": "^2.1.0", "request": "^2.72.0", "react-scroll":"^1.5.4", "url-parse": "^1.1.9" },
however gives me error:
error: not find "store" in either context or props of "connect(basecontainer)". either wrap root component in <provider>, or explicitly pass "store" prop "connect(basecontainer)".
if changed codes homepagehandler extends component directly, work.
what wrong codes? thanks!
render( <provider store={store}> <router history={history}> <route path="/" component={app}/> </router> </provider>, document.getelementbyid('app') );
can post content of entry point of app. should similar above code.
Comments
Post a Comment