2016-11-27 6 views
0

Blueprint UIライブラリには、ユーザーの操作に関する通知を表示するToasterコンポーネントがあります。ドキュメントから、最初Blueprintjsトーストコンポーネントのイディオムリアクション

MyToaster.show({message: 'some message'})続い

const MyToaster = Toaster.create({options})を呼び出すことで使われています。

showメソッドをReactのライフサイクルに適合させることができません - 異なるボタンのクリックで異なるメッセージを表示する再利用可能なトースターコンポーネントを作成するにはどうすればよいですか?役立つなら、私はMobXをデータストアとして使用しています。

+0

私は同じ問題を抱えています。私は、レントメソッドにMytoaster.showを部分的にifコントロールを入れて解決しました。私はReduxを使用し、エラーの小道具が変わるとMytoaster.showをレンダリングし、そうでない場合はnullを返します。今私の問題は、却下のアクションを起こすことです。 – Arcy

答えて

1

Toasterはあなたのリアクションライフサイクルを気にしないので、面白いものです。それはイベントに応じてすぐにトーストを焼くために命令的に使用されることを意図しています。

関連するイベントハンドラ(DOMクリックかReduxアクションのいずれか)でtoaster.show()と呼んでください。

我々は例自体でそれを行う方法を参照してください:Reduxの(とredux-actions)とtoastExample.tsx

0

私のソリューション...

処置:

export const setToaster = createAction('SET_TOASTER');

リデューサー:

const initialState = { 
    toaster: { 
    message: '' 
    } 
}; 

function reducer(state = initialState, action) { 
    switch(action.type) { 
    case 'SET_TOASTER': 
     return { 
     ...state, 
     toaster: { ...state.toaster, ...action.payload } 
     }; 
    }; 
} 

トースターコンポーネント:

// not showing imports here... 

class MyToaster extends Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     // set default toaster props here like intent, position, etc. 
     // or pass them in as props from redux state 
     message: '', 
     show: false 
    }; 
    } 

    componentDidMount() { 
    this.toaster = Toaster.create(this.state); 
    } 

    componentWillReceiveProps(nextProps) { 
    // if we receive a new message prop 
    // and the toaster isn't visible then show it 
    if (nextProps.message && !this.state.show) { 
     this.setState({ show: true }); 
    } 
    } 

    componentDidUpdate() { 
    if (this.state.show) { 
     this.showToaster(); 
    } 
    } 

    resetToaster =() => { 
    this.setState({ show: false }); 

    // call redux action to set message to empty 
    this.props.setToaster({ message: '' }); 
    } 

    showToaster =() => { 
    const options = { ...this.state, ...this.props }; 

    if (this.toaster) { 
     this.resetToaster(); 
     this.toaster.show(options); 
    } 
    } 

    render() { 
    // this component never renders anything 
    return null; 
    } 
} 

アプリケーションコンポーネント:

または任意のルートレベルのコンポーネントがある...

const App = (props) => 
    <div> 
    <MyToaster {...props.toaster} setToaster={props.actions.setToaster} /> 
    </div> 

いくつかの他のコンポーネント:

あなたはトースターを起動する必要があり

...

class MyOtherComponent extends Component { 
    handleSomething =() => { 
    // We need to show a toaster! 
    this.props.actions.setToaster({ 
     message: 'Hello World!' 
    }); 
    } 

    render() { ... } 
} 
+0

私はこれをかなり大きなコードベースで実装しようとしているので、これについていくつか質問があります。あなたは数分を持っていますか?あなたに私のメールを送ることができます@logik – joshuaaron

+0

@joshuaaron確かに、あなたの質問は何ですか? – logik

+0

[email protected]で私に電子メールを撃つことができますか?スクリーンショットの質問は役に立ちます。 – joshuaaron

関連する問題