我试图了解当我们在React中使用它们时各个函数调用如何工作
假设我们有两个这样的功能
const something = () => {
//something
}
function something1 () {
//something
}
到目前为止,我已经看到它们以三种不同的方式被调用
喜欢
{this.something}
this.something()
(() => something)
所以我的主要问题是它们有何不同?例如在被呼叫时以及是否有其他方法可以呼叫它们(此处未提及)?
答案 0 :(得分:2)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
private void btnScaleXIncrease_Click(Object sender, RoutedEventArgs e)
{
((ViewModel)DataContext).ScaleX++;
}
private void btnScaleXDecrease_Click(Object sender, RoutedEventArgs e)
{
((ViewModel)DataContext).ScaleX--;
}
private void btnScaleYIncrease_Click(Object sender, RoutedEventArgs e)
{
((ViewModel)DataContext).ScaleY++;
}
private void btnScaleYDecrease_Click(Object sender, RoutedEventArgs e)
{
((ViewModel)DataContext).ScaleY--;
}
}
将无法正确调用函数。 <Window x:Class="WpfApplicationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<local:ValueConverter x:Key="ValueConverter" />
</ResourceDictionary>
</Window.Resources>
<DockPanel>
<DockPanel DockPanel.Dock="Top">
<GroupBox Header="Scale X" DockPanel.Dock="Left" Width="100">
<StackPanel Orientation="Horizontal">
<Button Width="40" Click="btnScaleXIncrease_Click">+</Button>
<Button Width="40" Click="btnScaleXDecrease_Click">-</Button>
</StackPanel>
</GroupBox>
<GroupBox Header="Scale Y" DockPanel.Dock="Right" Width="100">
<StackPanel Orientation="Horizontal">
<Button Width="40" Click="btnScaleYIncrease_Click">+</Button>
<Button Width="40" Click="btnScaleYDecrease_Click">-</Button>
</StackPanel>
</GroupBox>
<StackPanel></StackPanel>
</DockPanel>
<ScrollViewer HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible">
<Grid Width="{Binding GridWidth, Mode=OneWay}" Height="{Binding GridHeight, Mode=OneWay}">
<Canvas Height="{Binding GridHeight, Mode=OneTime}" Width="{Binding GridWidth, Mode=OneTime}"
Background="Gray">
<Ellipse Fill="Yellow" Height="500" Width="500" StrokeThickness="2" Stroke="Black"/>
<Canvas.RenderTransform>
<ScaleTransform ScaleX="{Binding ScaleX}" ScaleY="{Binding ScaleY}"
CenterX="{Binding GridWidth, Converter={StaticResource ValueConverter}, ConverterParameter=0.5, Mode=OneTime}"
CenterY="{Binding GridHeight, Converter={StaticResource ValueConverter}, ConverterParameter=0.5, Mode=OneTime}" />
</Canvas.RenderTransform>
</Canvas>
</Grid>
</ScrollViewer>
</DockPanel>
</Window>
是调用它的理想方法,因为它是性能最高的!实际上,每次this.something
都会触发一次重新渲染,因为您正在创建一个新函数(实际上是将this.something()包装在一个空箭头函数中)。话虽如此,两者在不同情况下都是有用的。如有疑问,请使用this.something()
。