Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Redux connect not working

Tags:

react-redux

I am trying to use React+Redux in my codepen pen, but the connect is not injecting the prop,

Maybe its because being an amateur, I'm missing something. Please have a look. http://codepen.io/sahil28v/pen/EKEKME?editors=0010

const { Component } = React;
const { createStore, bindActionCreators, applyMiddleWare, combineReducers } =     Redux;
const { Provider, connect } = ReactRedux;

const Application = () =>  (
        <div className="ground">
    <Tmap />
        </div>
    );

class Tmap extends Component {
  constructor(props){
  super(props);
  console.log(this.props.mapstate);  // This is returning undefined,no idea why
  console.log(store.getState().mapstate); // Though this returns val: "hey" as it should properly though.
 }
 render () {
  return (
   <div>

   </div>
  );
 }
}

const mapStateToProps = (state) => ({
mapstate: state.mapstate
});

connect(mapStateToProps)(Tmap);

const initialState = {
  val: "hey"
}

const mapReducer = (state = initialState, action) => {
 return state ;
} ;

const rootReducer = combineReducers({
  mapstate: mapReducer,
});

const store = createStore(rootReducer);

React.render(
    <Provider store={store}>
        <Application />
    </Provider>, document.getElementById('app')
);

Also,,, can I configure store over here in codepen itself for using redux devtools(chrome extension). I tried adding ...window.devToolsExtension ? window.devToolsExtension() : f => f , to the create store but that doesn't work.

While somebody is answering -- can you also recommend good documentation/video for: ->learning ES6 ...

-- Thanks for the help

like image 846
user4076248 Avatar asked Apr 14 '16 22:04

user4076248


People also ask

Why react Redux is not working?

Make sure you don't have a duplicate instance of React on the page. Make sure you don't have multiple instances/copies of React Redux in your project. Make sure you didn't forget to wrap your root or some other ancestor component in <Provider> . Make sure you're running the latest versions of React and React Redux.

How Connect works in react redux?

The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.

What is the use of the connect () function in React?

The React Redux connect() API is used for creating container elements that are connected to the Redux store. The Redux store is derived from the topmost ancestor of the component using React Context. If you are only creating a presentational component, you have no need for connect() .

Can I use Redux connect with React hooks?

We recommend using the React-Redux hooks API as the default approach in your React components. The existing connect API still works and will continue to be supported, but the hooks API is simpler and works better with TypeScript.


2 Answers

Connect returns a new instance of the component I suppose. This works, using connect as a decorator.

@connect(
  state => ({
    mapstate: state.mapstate 
  })
)
class Tmap extends Component {
  constructor(props){
    super(props);
    alert(this.props.mapstate);  // This is returning undefined,no idea why
    alert(store.getState().mapstate); //As I understand this is the direct low level api which connect is meant to abstract away along with store.subscribe.
  }

  render () {
    return (
    <div>
    </div>
    );
  }
}
like image 107
Guillaume Badi Avatar answered Sep 21 '22 12:09

Guillaume Badi


You have forgotten to save the result of connect into a new variable. And this new variable is what you should use instead of Tmap.

const CTmap = connect(... 
.... 
<CTmap/>
like image 26
Leif John Avatar answered Sep 23 '22 12:09

Leif John