我一直在玩Redux,但我似乎无法与React联系。我正在使用connect
和mapStateToProps
,但我商店中的道具没有传递到我的组件中。也许有人可以阐明为什么我的代码会起作用?我猜它与mapStateToProps函数有关。
这是我所拥有的代码:
import { connect, Provider } from 'react-redux';
import { createStore, combineReducers } from 'redux';
import React from 'react';
import ReactDOM from 'react-dom';`enter code here`
const greetReducer = ( state = '', action ) => {
if ( action.type === 'greet' ) {
state = action.greeting
}
return state;
};
const runReducer = ( state = '', action ) => {
if ( action.type === 'run' ) {
state = action.running
}
return state;
};
const reducers = combineReducers({
greetState: greetReducer,
runState: runReducer
});
const store = createStore( reducers );
store.dispatch({
type: 'greet',
greeting: 'Hello'
});
console.log(store.getState());
store.dispatch({
type: 'run',
running: 'Running like the wind'
});
const App = (props) => <h1>{props.greeting}</h1>;
const mapStateToProps = function(store) {
return {
greeting: store.getState().greetState
};
}
connect(mapStateToProps)(App);
ReactDOM.render(
<Provider store={store}><App /></Provider>,
document.getElementById('root')
);
答案 0 :(得分:6)
$ sudo perl -E 'say for $ENV{USER}, $ENV{SUDO_USER}'
函数(或者更确切地说,connect
返回的函数)不会修改您的组件;它返回一个新组件,您需要将其保存到变量:
connect