Reputation: 815
I am using Redux for state management. I am getting the error that the props are undefined. Below blocks shows my code,
index.js
ReactDOM.render(
<Provider store={configureStore()}>
<App />
</Provider>,
document.getElementById('root')
);
store.js
function configureStore(state = { rotating: true }) {
return createStore(rotateReducer,state);
}
Browser console:
App {props: undefined, context: undefined, refs: {…}, updater: {…}}
on expand this App object in console,
props: {rotating: true, startAction: ƒ, stopAction: ƒ} (now props are defined).
App.js
function App() {
return (
<div className="App">
<header className="App-header">
<img
src={logo}
className={
"App-logo" +
(this.props.rotating ? "":" App-logo-paused")
}
alt="logo"
onClick={
this.props.rotating ?
this.props.stopAction : this.props.startAction
}
/>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
what is the issue on props to App component load?
Upvotes: 0
Views: 297
Reputation: 10579
Remove the this
keyword before the props, it's a functional component and this
is used in class
components only.
And add props parameter in the function signature.
Try this
Example:
import React from "react";
import { connect } from "react-redux";
function App(props) {
return (
<div className="App">
<header className="App-header">
<img
src={logo}
className={
"App-logo" + (props.rotating ? "" : " App-logo-paused")
}
alt="logo"
onClick={
this.props.rotating ? props.stopAction : props.startAction
}
/>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
const mapStateToProps = state => ({ ...state });
const mapDispatchToProps = dispatch => ({
startAction: () => dispatch(startAction),
stopAction: () => dispatch(stopAction)
});
export default connect(mapStateToProps, mapDispatchToProps)(App);
Upvotes: 1