美文网首页初见
react状态提升

react状态提升

作者: 佛本非佛 | 来源:发表于2018-07-14 14:03 被阅读0次

状态提升

官网摄氏度与华氏度转化的例子

// class BoilingVerdict extends React.Component{
// constructor(props){
// super(props)
// }
// render(){
// if(this.props.celsius>=100){
// return <p>The water would boil.</p>;
// }
// return <p>The water would not boil.</p>;
// }
// }
// class Calculator extends React.Component{
// constructor(props){
// super(props);
// this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
// this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
// this.state = {temperature: '', scale: 'c'};
// }
// handleCelsiusChange(temperature) {
// this.setState({scale: 'c', temperature});
// }

// handleFahrenheitChange(temperature) {
// this.setState({scale: 'f', temperature});
// }
// render(){
// const scale = this.state.scale;
// const temperature=this.state.temperature;
// const celsius=scale==='f'?tryConvert(temperature,toCelsius):temperature;
// const fahrenheit=scale==='c'?tryConvert(temperature,toFahrenheit):temperature;
// return (
// <div>
// <TemperatureInput scale="c"
// temperature={celsius}
// onTemperatureChange={this.handleCelsiusChange}
// />
// <TemperatureInput scale="f"
// temperature={fahrenheit}
// onTemperatureChange={this.handleFahrenheitChange}
// />
// <BoilingVerdict
// celsius={parseFloat(celsius)} />
// </div>
// )
// }
// }
// function toCelsius(fahrenheit) {
// return (fahrenheit - 32) * 5 / 9;
// }

// function toFahrenheit(celsius) {
// return (celsius * 9 / 5) + 32;
// }
// function tryConvert(temperature, convert) {
// const input = parseFloat(temperature);
// if (Number.isNaN(input)) {
// return '';
// }
// const output = convert(input);
// const rounded = Math.round(output * 1000) / 1000;
// return rounded.toString();
// }
// const scaleNames = {
// c: 'Celsius',
// f: 'Fahrenheit'
// };
// class TemperatureInput extends React.Component {
// constructor(props) {
// super(props);
// this.handleChange = this.handleChange.bind(this);
// this.state = {temperature: ''};
// }

// handleChange(e) {
// // this.setState({temperature: e.target.value});
// this.props.onTemperatureChange(e.target.value);
// }

// render() {
// // const temperature = this.state.temperature;
// const temperature = this.props.temperature;
// const scale = this.props.scale;
// return (
// <fieldset>
// <legend>Enter temperature in {scaleNames[scale]}:</legend>
// <input value={temperature}
// onChange={this.handleChange} />
// </fieldset>
// );
// }
// }
// ReactDOM.render(<Calculator/>, document.getElementById('root'));

转载http://react.css88.com/docs/lifting-state-up.html

支出人民币和美元换算demo

class MoneyMax extends React.Component{
constructor(props){
super(props);
}
render(){
if(this.props.money>5000){
return <p>"本月超出预算"</p>
}
return <p>"本月未超出预算"</p>
}
}
class MoneyBox extends React.Component{
constructor(props){
super(props);
this.handleRmbChange = this.handleRmbChange.bind(this);
this.handleDollarChange = this.handleDollarChange.bind(this);
this.state={scale:'r',money:''}
}
handleRmbChange(money){
this.setState({scale:'r',money})
}
handleDollarChange(money){
this.setState({scale:'d',money})
}
render(){
const scale=this.state.scale;
const money=this.state.money;
const rmb=scale==='d'?tryConvert(money,toRmb):money;
const dollar=scale==='r'?tryConvert(money,toDollar):money;
console.log(dollar)
return (
<div>
<MoneyInput
scale="r"
money={rmb}
moneyChange={this.handleRmbChange}
/>
<MoneyInput
scale="d"
money={dollar}
moneyChange={this.handleDollarChange}
/>
<MoneyMax money={money}/>
</div>
)
}
}
function toRmb(dollar) {
return dollar/7;
}

function toDollar(rmb) {
return rmb*7;
}
function tryConvert(money, convert) {
const input = parseFloat(money);
if (Number.isNaN(input)) {
return '';
}
const output = convert(input);
const rounded = Math.round(output * 1000) / 1000;
return rounded.toString();
}
const scaleNames = {
r: 'Rmb',
d: 'Dollar'
};
class MoneyInput extends React.Component{
constructor(props){
super(props);
this.handleChange=this.handleChange.bind(this);
this.state = {money: ''};
}
handleChange(e){
this.props.moneyChange(e.target.value)
}
render(){
const money=this.props.money;
const scale = this.props.scale;
return (
<fieldset>
<legend>输入花销{scaleNames[scale]}:</legend>
<input value={money}
onChange={this.handleChange} />
</fieldset>
);
}
}
ReactDOM.render(<MoneyBox/>, document.getElementById('root'));


每月花销效果预览图

相关文章

  • react 状态提升

    状态提升是什么意思? 一个最简单的例子,就是如果两个子组件需要利用到对方的状态的话,那么这个时候我们就需要使用到状...

  • react状态提升

    状态提升 官网摄氏度与华氏度转化的例子 // class BoilingVerdict extends React...

  • React 状态提升

    自组件通过props来接收父组件传递的信息,但是子组件不能更改props,子组件可以定义state,state可以...

  • React状态提升

    通常,多个组件需要反映相同的变化数据,这时通常将共享状态提升到最近的共同父组件中去。 在 React 应用中,任何...

  • React 状态提升

    在不使用任何状态管理方案的前提下,React 组件之间共享状态,只能靠 props 传播。 父->子的传播非常容易...

  • react的状态提升

    在react中是单向数据流的设计,即只有父组件可以传递数据给子组件,而没有子组件传递数据给父组件的概念.以正确的技...

  • React笔记10(状态提升)

    状态提升 使用React经常会遇到几个组件需要共用状态数据的情况。这种情况下,我们最好把这部分状态数据提升到他们最...

  • React中的状态提升

    React中的状态提升概括来说,就是将多个组件需要共享的状态提升到它们最近的父组件上.在父组件上改变这个状态然后通...

  • React.js 小书 Lesson17 - 前端应用状态管理

    React.js 小书 Lesson17 - 前端应用状态管理 —— 状态提升 本文作者:胡子大哈本文原文:htt...

  • React学习笔记(11)状态提升

    通常,多个组件需要反映相同的变化数据,这时我们建议将共享状态提升到最近的共同父组件中去。 两个组件都要用到同一个数...

网友评论

    本文标题:react状态提升

    本文链接:https://www.haomeiwen.com/subject/iecypftx.html