react:初始化redux状态的值

时间:2016-10-31 19:27:10

标签: reactjs redux

我正在尝试使用redux状态的默认值初始化组件。 以下是调用根节点的代码:

const str="Italy quake: Norcia tremor destroys ancient buildings";
const words = str.split(" ");
const preloadedState = { randomWords: {
  activeWord:0,
  activeLetter:0,
  words:words
}};

ReactDOM.render(
  <Provider store={createStore(root_reducer,preloadedState)}>
      <App />
  </Provider>,document.getElementById('root')
);

和combineReducers是:

import {combineReducers} from 'redux';
import RandomWords from './RandomWordsReducer';
import Score from './ScoreReducer';

const root_reducer=combineReducers({
    randomWords:RandomWords,
    score:Score
});


export default root_reducer;

在容器中,我已将组件与redux连接: (我确信组件和redux已连接)

 function mapStateToProps(state){

        return ({words:state.words,
                activeLetter:state.activeLetter,
                activeWord:state.activeWord});
    }

    export default connect(mapStateToProps)(RandomWords);

然后在组件上我有:

export default class RandomWords extends Component{

      render(){
        console.log("words are:"+this.props.words);
          return(<div>
              {  this.props.words }
            </div>)
      }
}

问题是: 在console.log("words are:"+this.props.words); 行中,我希望看到一系列单词,同时它是未定义的。

代码有什么问题?

1 个答案:

答案 0 :(得分:1)

没有state.words,只有state.randomWords.words

return ({words:state.randomWords.words,
        activeLetter:state.randomWords.activeLetter,
        activeWord:state.randomWords.activeWord});