Javascript ={……this.markedDays:{},someVar:0};this.handler=this.handler.bind(this);this.markedDays={};}你的意思是做this.state={….this.markedDays:{
Javascript ={……this.markedDays:{},someVar:0};this.handler=this.handler.bind(this);this.markedDays={};}你的意思是做this.state={….this.markedDays:{,javascript,react-native,components,rendering,parent-child,Javascript,React Native,Components,Rendering,Parent Child,={……this.markedDays:{},someVar:0};this.handler=this.handler.bind(this);this.markedDays={};}你的意思是做this.state={….this.markedDays:{}?你的意思是this.state={….markedDays:{}?还是我应该在状态中使用它?构造函数(){super();this.state={definestatevariablesher:true}你不应该这样做。state.mark
={……this.markedDays:{},someVar:0};this.handler=this.handler.bind(this);this.markedDays={};}你的意思是做
this.state={….this.markedDays:{}
?你的意思是this.state={….markedDays:{}
?还是我应该在状态中使用它?构造函数(){super();this.state={definestatevariablesher:true}你不应该这样做。state.markedDays=markedDaysHolder。始终使用setState更新状态对象。使用setState重新呈现组件和所有子组件。感谢大家的关注,我做了更改,但运气不好,用我发现的其他东西更新我的问题使用React.PureComponent并在其中定义你的状态构造函数构造函数(){super();this.state={……this.markedDays:{},someVar:0};this.handler=this.handler.bind(this);this.markedDays={};}你的意思是做this.state={……this.markedDays:{}
?你的意思是this.state={……markedDays:{}
?还是我应该在状态中使用它?构造函数(){super();this.state={defineStateVariablesHere:true}
export default class InitiateRent extends Component {
state = {
....
this.markedDays: {},
someVar: 0
};
constructor() {
super();
this.handler = this.handler.bind(this);
this.markedDays = {};
}
....
handler(e) {
console.log('handler running');
this.setState({
someVar: 123
})
}
<Calendar
handler={this.handler}
markedDates={this.markedDays}
onDayPress={(day)=> {
console.log('selected day', day);
var dateString = `'${day.year}-${day.month}-${day.day}'`
var addDate = {
[dateString]: {
customStyles: {
container: {
backgroundColor: '#6de3dc',
},
text: {
color: 'white',
fontWeight: 'bold'
},
},
}
}
this.markedDays = Object.assign(addDate, this.markedDays);
console.log(this.markedDays);
}}
....
pressDay(date) {
this.props.handler();
this._handleDayInteraction(date, this.props.onDayPress);
}
renderDay(day, id) { //GETS CALLED IN CHILD RENDER METHOD VIA AN INTERMEDIARY
....
<DayComp
....
onPress={this.pressDay}
....
}
Object
'2018-9-23': {customStyles: {container: {backgroundColor: "#6de3dc"}, text: {color: "white", fontWeight: "bold"}}}
'2018-9-26': {customStyles: {container: {backgroundColor: "#6de3dc"}, text: {color: "white", fontWeight: "bold"}}}
'2018-9-28': {customStyles: {container: {backgroundColor: "#6de3dc"}, text: {color: "white", fontWeight: "bold"}}}
'2018-9-29': {customStyles: {container: {backgroundColor: "#6de3dc"}, text: {color: "white", fontWeight: "bold"}}}
const markedDays = this.state.markedDays;
var dateString = `'${day.year}-${day.month}-${day.day}'`;
var addDate = {
[dateString]: {
customStyles: {
container: {
backgroundColor: '#6de3dc',
},
text: {
color: 'white',
fontWeight: 'bold'
},
},
}
}
const markedDaysHolder = {
...markedDays,
...addDate
}
this.state.markedDays = markedDaysHolder;
componentDidUpdate(prevProps, prevState, snapshot) {
console.log("componentDidUpdate in InitiateRent:", prevProps, prevState, snapshot);
}
componentDidUpdate in InitiateRent: (3) (index.bundle, line 761)
{initiateRentMessage: function, modalHeight: 200, modalWidth: 200, handler: function}
{modalVisible: true, message: "Hi, I would like to rent an item from you.", rentButtonBackground: "#6de3dc", someVar: 123, markedDays: Object}
undefined
constructor(props) {
super(props)
this.state = {
}
this.onDayPress = this.onDayPress
}
showCalendar = () => {
return (
<Calendar
onDayPress={this.onDayPress}
style={styles.calendar}
hideExtraDays
markedDates={{
[this.state.selected]: {
selected: true,
disableTouchEvent: true,
selectedDotColor: 'orange',
},
}}
/>
)
}
onDayPress = day => {
this.setState({
selected: day.dateString,
})
}
....
render() {
let containerStyle = [this.style.base];
let textStyle = [this.style.text];
let marking = this.props.marking || {};
if (marking && marking.constructor === Array && marking.length) {
marking = {
marking: true
};
}
const isDisabled = typeof marking.disabled !== 'undefined' ? marking.disabled : this.props.state === 'disabled';
console.log('marking in day:', marking.selected);
if (marking.selected) {
containerStyle.push(this.style.selected);
} else if (isDisabled) {
textStyle.push(this.style.disabledText);
} else if (this.props.state === 'today') {
containerStyle.push(this.style.today);
textStyle.push(this.style.todayText);
/********ADD THIS CONDITION********/
} else if(!marking.selected) {
textStyle.push({backgroundColor: '#ffffff', color: '#2d4150'});
}
....