我有以下組成部分:設置狀態不會重新呈現部件的反應,和/ ES6
import React from 'react';
import styles from './ShareModal.css';
import { observer } from 'mobx-react';
// Components
import Modal from '../Modal/Modal';
import Button from '../Button/Button';
import Input from '../Input/Input';
// Stores
import UiStore from '../../stores/UiStore';
@observer
class ShareModal extends React.Component {
constructor() {
super();
this.state = {
inputs : [
{ type: 'email', placeholder: 'Email Address' }
]
};
}
addInput() {
// DEBUG
console.log('Adding an input...');
// this.state.inputs = this.state.inputs.concat([
// { type: 'email', placeholder: 'Email Address' }
// ]);
this.setState(this.state.inputs.concat([
{ type: 'email', placeholder: 'Email Address' }
]));
}
render() {
return (
<div className={ UiStore.state.shareVisible ? styles.visible : styles.hidden }>
<Modal id={'share'}>
<div className={styles.content}>
<h1 className={styles.title}>Share</h1>
<p className={styles.description}>Share with as many friends as you want. Add their email addressess below</p>
{
// Itterates over all inputs in the current state
this.state.inputs.map((item, i) => (
<Input key={i} type={item.type} placeholder={item.placeholder} />
))
}
<Button
icon = {'add'}
color = {'#FC3839'}
type = {'outline'}
text = {'Add Another Email Address'}
width = {'full'}
rounded = {false}
action = {this.addInput.bind(this)}
/>
<Button
color = {'#FC3839'}
type = {'full'}
text = {'Share'}
width = {'full'}
rounded = {true}
/>
</div>
</Modal>
</div>
);
}
}
export default ShareModal;
我卡上的addInput
功能。你可以收集當該函數運行時應該發生的事情...所以在按鈕上單擊它會運行(它會這樣做),然後應該在this.state.inputs
中添加另一個輸入,但它似乎不工作。也沒有錯誤顯示。
耶的狀態應該是一個對象,OP的代碼試圖將其設置爲一個數組 – Jayce444