2017-04-19 18 views
0

我有一個反應組件,它顯示了一個消息:,呼叫作出反應組件顯示像一個警報功能

下面的代碼:

import React, { PropTypes } from 'react'; 

const Message = props => { 
    const { type, msg } = props; 

    if (type === 'success') { 
    return (<div>{msg}</div>); 
    } else { 
    return null; 
    } 
}; 

Message.PropTypes = { 
    type: PropTypes.string.isRequired, 
    msg: PropTypes.string.isRequired, 
}; 

export default Message; 

//這一部分稱爲像這樣從指標。 js:

<Message type="success" msg="This is a Message" /> 

我的問題是...我怎樣才能調用組件,就像我會調用一個函數。

例如:

if (function is success then) { 
    //Show the Message Component 
} 

我怎樣才能做到這一點與反應是?

回答

2

如果if子句中相互反應成分你只是使它,

class AnotherReact extends Component { 
    render() { 
     let alert = success ? <Message /> else ''; 
     return (<div>{ alert }</div>); 
    } 
} 

否則,如果不是在一個陣營組成部分,那麼你將不得不使用ReactDOM.render()

if (is success) { 
    ReactDOM.render(<Message />, document.querySelector()); 
} 
+0

怎麼樣與一個onclick事件? – JakeBrown777

+0

@ JakeBrown777你是什麼意思? –