【发布时间】:2018-03-01 21:27:31
【问题描述】:
这就是我正在使用的:
反应:15.6.1
触发onChange时发生错误。
组件文件:
import React, { Component } from 'react';
var defaults = {
text: {
placeholder: '',
label: ''
},
style: {
wrapper: {
display: 'block',
padding: '9px 0'
},
label: {
display: 'block',
padding: '0px 8px 4px',
color: require('macros').theme.text.info
},
input: {
display: 'block',
position: 'relative',
boxSizing: 'border-box',
border: 0,
outline: 0,
// border: `1px solid ${ require('macros').theme['text field'].border }`,
width: '100%',
padding: '12px 6px',
fontSize: '16px',
background: require('macros').theme['text field'].background,
color: require('macros').theme.text.info
},
active: {
input: require('macros').theme['text field'].active.background
}
},
type: 'text',
onChange: function() {}
};
export default class Nav extends Component {
constructor(props) {
super(props)
var component = this;
component.state = require('venatici').object.combine(defaults, component.props);
component.onChange = component.onChange.bind(this);
}
onChange(event) {
var component = this;
component.state.onChange(event.target.value);
component.state.style.input.background = '#494949';
component.forceUpdate();
}
render() {
var component = this;
return (
<text-field
ref='outer'
style={ component.state.style.wrapper }
>
<label style={ component.state.style.label }>{ component.state.text.label }</label>
<input
name={ component.state.type }
type={ component.state.type }
style={ component.state.style.input }
placeholder={ component.state.text.placeholder }
onChange={ component.onChange }
></input>
</text-field>
);
}
componentDidMount() {
}
}
错误
警告:input 传递了一个先前已更改的样式对象。不推荐使用突变 style。考虑事先克隆它。检查Nav 的render。以前的样式:{display: "block", position: "relative", boxSizing: "border-box", border: 0, outline: 0, width: "100%", padding: "12px 6px", fontSize: "16px ",背景:"#333",颜色:"#eee"}。变异样式:{display:“block”,position:“relative”,boxSizing:“border-box”,border:0,outline:0,width:“100%”,padding:“12px 6px”,fontSize:“16px ",背景:"#494949",颜色:"#eee"}。
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
不要直接改变状态。 facebook.github.io/react/docs/…
标签: javascript reactjs