2017-06-20 78 views
0

我是新來的React。我創建了一個演示應用程序。並且我試圖通過單擊按鈕來隱藏該消息。 1)但是,該操作不會調用按鈕單擊。 2)爲什麼控制檯在減速器內部控制action時顯示如下。這顯示在頁面重新加載。 view page行動不會在按鈕點擊reactjs

inside reducer 
redux.js:30 Object {type: "@@redux/INIT"} 
redux.js:31 Object {} 
redux.js:29 inside reducer 
redux.js:30 Object {type: "@@redux/PROBE_UNKNOWN_ACTION_j.r.h.g.s.q.b.y.b.9"} 
redux.js:31 Object {} 
redux.js:29 inside reducer 
redux.js:30 Object {type: "@@redux/INIT"} 
redux.js:31 Object {} 

我的package.json

{ 
    "name": "react-redux-data-flow", 
    "version": "0.1.0", 
    "private": true, 
    "dependencies": { 
    "react": "^15.6.1", 
    "react-dom": "^15.6.1", 
    "react-redux": "^5.0.5", 
    "redux": "^3.7.0", 
    "redux-thunk": "^2.2.0" 
    }, 
    "devDependencies": { 
    "react-scripts": "1.0.7" 
    }, 
    "scripts": { 
    "start": "react-scripts start", 
    "build": "react-scripts build", 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject" 
    } 
} 

App.js

import React, { Component } from 'react'; 
import logo from './logo.svg'; 
import './App.css'; 
import { initialMessageAction, clickButtonAction } from './redux.js'; 
import { connect } from 'react-redux'; 


class App extends Component { 
    render() { 
    return (
     <div className="App"> 
     <div className="App-header">   
     </div> 
     <p className="App-intro"> 
     react-redux-data-flow 
     </p> 
      {this.props.message? <div>hi ..<button onClick={() => this.props.clickButtonAction }>hide message</button></div>: ''} 
     </div> 
    ); 
    } 
} 

// mapStateToProps......................................................................................................... 

    const mapStateToProps = (state, ownProperty) => ({ 

    message:state.geod 

    }); 
//............................................................................................................................... 


// DispatchStateToProps......................................................................................................... 

    const mapDispatchToProps = { 

    // initialMessageAction, 
    clickButtonAction 

    } 

// connect to Store............................................................................................................................... 

    export default connect(
          mapStateToProps, 
          mapDispatchToProps 

        )(App); 

// ..................................................................................................................................................................... 

redux.js

import React from 'react'; 
import { createStore, combineReducers, applyMiddleware } from 'redux'; 
import thunk from 'redux-thunk'; 

//actions.js.............................................................................................. 


export const initialMessageAction = (geod) => ({ 

                type:'INITIAL_MESSAGE', 
                geod, 

}); 

export const clickButtonAction =() =>{  
              console.log('inside click button action'); 
              return { 

                 type:'CLICK_MESSAGE', 

                } 
}; 

//......................................................................................................... 

//reducer.js......................................................................................................... 

export const geod = (state ={}, action) => { 
    console.log('inside reducer'); 
    console.log(action); 
    console.log(state) 
    switch(action.type){ 


     case 'INITIAL_MESSAGE': 
       console.log('inside reducer'); 
       return action.geod; 
     case 'CLICK_MESSAGE': 
       return [ 
         ...state, { 
             message: '' 
            } 
         ] 
     default: 
       return state; 
    } 

}; 
//......................................................................................................... 

//reducer.js......................................................................................................... 

    export const reducers = combineReducers({ geod, }); 
//......................................................................................................... 

//store.js......................................................................................................... 

    // export const store = createStore(reducers, applyMiddleware(thunk)); 
//......................................................................................................... 

export function configureStore(initialState = {}) { 
    const store = createStore(
    reducers, 
    initialState, 
    applyMiddleware(thunk) 
) 
    return store; 
}; 

export const store = configureStore(); 

index.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 
import registerServiceWorker from './registerServiceWorker'; 
import './index.css'; 
// Add these imports - Step 1 
import { Provider } from 'react-redux'; 
import { store } from './redux'; 
// ReactDOM.render(<App />, document.getElementById('root')); 
// registerServiceWorker(); 

// Wrap existing app in Provider - Step 2 
ReactDOM.render( 
    <Provider store={store}> 
    <App /> 
    </Provider>, 
    document.getElementById('root') 
); 
+0

您發佈的日誌只是來自Redux的日誌,讓您知道哪些操作正在分派。此處記錄的操作來自Redux本身,在初始化商店時調度。 – ArneHugo

回答

1

您的錯誤非常小。在您的線路:

<button onClick={() => this.props.clickButtonAction}>... 

你說是單擊按鈕時,調用,它返回this.props.clickButtonAction功能。您可以通過兩種方式解決這個問題:

<button onClick={this.props.clickButtonAction}>... 

<button onClick={() => this.props.clickButtonAction()}>... 

第一個傳遞你要調用(this.props.clickButtonAction)到onClick道具的功能。第二個傳遞一個函數,當被調用時,將調用this.props.clickButtonAction

使用第一個更好,因爲它更短,並且不會產生額外的不必要的功能,但當您想傳遞自定義參數(onClick={() => innerFuction(customArgument)})時,第二個解決方案很有用。

+0

感謝您的回覆... –

+0

現在,在reducer的'​​clickButtonAction'動作調用按鈕click.But在第一個按鈕單擊返回的對象是'{}'。第二次點擊按鈕返回對象。 '內減速器 redux.js:30對象{類型: 「CLICK_MESSAGE」} redux.js:31對象{} redux.js:29內減速 redux.js:30對象{類型: 「CLICK_MESSAGE」} redux.js:31 [Object]'但消息和按鈕仍然顯示 –

+0

我想你可能會誤解你的商店行爲,因爲你的日誌記錄有點混亂。修復日誌記錄或將[redux-logger](https://github.com/evgenyrodionov/redux-logger)添加到您的商店,以更好地查看每個操作如何更改狀態。 – ArneHugo

2

1)在按鈕事件處理函數分配微小的錯誤

onClick={() => this.props.clickButtonAction }將簡單地返回回調,什麼也不做

onClick={this.props.clickButtonAction}回調指定爲事件處理程序

2)他們只是看起來像linting警告

+0

謝謝,很多.......... –

+0

@alenchill我使用了''redux-thunk'中間件。是否需要從動作創建者調用reducer? –

+0

@sojan不,你不需要它的基本行動創造者,如你有。 redux-thunk可以在一次調用中向動作創建者分配多個動作,並且在進行異步網絡調用以多次更新存儲狀態以便跟蹤我們是否(a)正在等待響應時經常使用,(b)已收到答覆或(c)收到錯誤。 – ArneHugo