score:0
if i am not wrong you want to implement something known as uncontrolled components. if so i will suggest you to implement it in following way.
class page extends react.component{
static getinitialprops(){
return {foo:"some value"}
}
render(){
return <provider store={createstore(reducer,{foo:this.props.foo})}>
<app/>
</provider>
}
}
then your app.js will be
@connect(mapstatetoprops,{dispatchfoo})
class app extends react.component{
componentdidmount(){
this.props.dispatchfoo({foo:"some new value"});
}
render(){
<div>{this.props.foo}</div>
}
}
score:0
you have 2 options:
1. use defaultprops
defaultprops can be defined as a property on the component class itself, to set the default props for the class. this is used for undefined props, but not for null props. for example:
app.defaultprops = {
foo: true
};
see defaultprops from react's blog.
2. setup an initial state
inside your reducer you can set your state initial values, that values will be available via mapstatetoprops
:
const initialstate = {
foo: false
};
export default function(state = initialstate, action) {
console.log('action', action);
switch (action.type) {
case types.some_action:
return {
...state,
foo: true
};
case types.another_action:
return {
...state,
foo: false
};
default:
return state;
}
}
in general, i don't see any point of overriding the same props inside mapstatetoprops
as it should prevent your app from being updated by redux.
Source: stackoverflow.com
Related Query
- NextJS: Best way to handle Connect / OwnProps / GetInitialProps
- React + Redux - What's the best way to handle CRUD in a form component?
- React - What is the best way to handle login and authentication?
- Best way to handle undefined values in ReactJS?
- ReactJS + Socket.IO - Best way to handle socket connection
- Best way to handle loading, success and error in redux
- React best way to handle empty object before mount?
- Best way to handle useRef(null) in typescript
- Best way to navigate between pages and handle back button in react js
- The best way to pass authorization header in nextJs using Apollo client? ReferenceError: localStorage is not defined
- Best way to handle images and links from a rest api and displaying them in React
- Right way to handle error in getServerSideProps in Nextjs
- Best way to handle complex State in React
- Whats the best way to handle Nav item selection?
- Best Way to Handle Repeated API Queries With Data Limit Restrictions
- redux - Best way to handle action calls out of component while on 'dirty' state?
- What's the best way to handle multiple languages via React Router v5?
- Best way to handle conditional parent containers in React?
- Best way to handle two similar type of useEffect functions in React?
- best way to handle fetching Status in redux
- With React what is the best way to handle conditional classes
- What is the best way to handle default values in controlled components?
- What is the best way to handle sorting data that is passed from props?
- What is the best way to connect React js app with node js server?
- best way to handle conditions in React
- What is the best way to handle Firestore with React?
- What is the best way to handle enum prop in styled-components
- What is best way to handle .env api urls, based on enviroments (dev / prod) REACT?
- what is the best way to handle error in useMutation hook
- Best way to handle component loading in react
More Query from same tag
- React componentDidUpdate lifecycle enters with null prevState
- How to override anchor <a> element in material ui v3?
- Promise after Promise after Promise
- How to Cache API data using AsyncStorage React Native
- How to use var element with value-less attribute in ReactJS JSX?
- Allow direct import of files within npm module like lodash
- Why is setState not working when using FileReader to read file content?
- Is there any way to block user navigation with out any prompt in react
- In React, try to show different images according to the IDs passed to url
- React router not rendering the component on click of a Array item
- setInterval in UseEffect getting call 2/4 times
- React ref syntax and components as pure functions
- MUI Modal [tabindex]?
- Are inline styles considered to be a bad practice?
- Enum index signatures in TypeScript/React
- React useEffect hook and Async/await own fetch data func?
- React How to render a component after onClick the correct way?
- Replace all html style attributes with React compatible styles
- How can I get the role of an element through the evt object? Or any other distinguishable properties really
- Make a new div element for each element in an array with JSX?
- React + Ant design login form e.preventDefault is not a function
- React: Reorder children components
- useState array and async scope lookup
- React - Using async with stripe createPaymentMethod
- Require and UnRequire a file dynamically
- ReactJS: How to remove row highlight on <TableRow> of Material-UI's <Table>?
- How to use external JavaScript functions in ReactJs component?
- Active class for React.js buttons
- What is the best way to delay re-render for controlled components in React JS?
- How can I change the default port in react from 3000 to another port?