reactjs - Failed PropType. Value is undefined -
my intention basic. want name appear in hellopage container upon rendering and, then, click turn name "bob." presume i'm missing stupid.
hellopage.js (container, name should appear, totally undefined)
import react 'react'; import proptypes 'prop-types'; import {connect} 'react-redux'; import {bindactioncreators} 'redux'; import * actions '../actions/helloactions'; export const hellopage = ({name, actions}) => { return ( <div> name {name}. hello page. <div onclick={() => actions.sayhello()}>but name different.</div> </div> ); }; hellopage.proptypes = { actions: proptypes.object.isrequired, name: proptypes.string.isrequired }; function mapstatetoprops(state) { return { name: state.name }; } function mapdispatchtoprops(dispatch) { return { actions: bindactioncreators(actions, dispatch) }; } export default connect(mapstatetoprops, mapdispatchtoprops)(hellopage);
i'm guessing culprit in reducer set initial state. initial state not set enough? part of code right?
import { say_hello } '../constants/actiontypes'; import objectassign 'object-assign'; import initialstate './initialstate'; export default function helloreducer(state = initialstate.hello, action) { switch (action.type) { case say_hello: return objectassign({}, state, { name: "bob" }); default: return state; } }
this way have initialstate file set up:
export default { hello: { name: 'gabriel' } };
and, way i'm combining reducers:
import { combinereducers } 'redux'; import fuelsavings './fuelsavingsreducer'; import hello './helloreducer'; import {routerreducer} 'react-router-redux'; const rootreducer = combinereducers({ fuelsavings, hello, routing: routerreducer }); export default rootreducer;
the other containers/components of app working perfectly, new container isn't. i'm using react slingshot boilerplate. added 1 route. copied structure there in boilerplate.
the initial state 'name' defined @ second level of object 'hello'.
so access it,you need change mapstatetoprops function as:
function mapstatetoprops(state) { return { name: state.hello.name }; }
Comments
Post a Comment