在React.js中,从一个函数中使用另一个函数内的元素。

huangapple go评论79阅读模式
英文:

using an element from an function inside of another function in React js

问题

第一个函数是这样的,我想在第二个函数中使用计数:

const ProductListItem = ({
  item: {id, title, description, price, currency, images, category}, 
  imageSize,
  margins,
  onPress,
}) => {
    const [count, setCount] = useState(0);

    const countAddHandler = () => {
      setCount(count + 1)
    }
    const countSubtractHandler = () => {
      setCount(count - 1)
    }
    const countNumHandler = () => {
      if (count >= 0) {
        return count
      }
      else
      setCount(0)
      return count
    }

  return ( 
    // 等等...
  )
}

这是第二个函数:

export default function OrderCheckoutScreen({}) {
  const navigation = useNavigation();
  const dispatch = useDispatch();

  const {selected_products, loading, success, message} = useSelector(
    state => state.products.orderCheckout,
  ); 
  // 等等...
}

请注意,它们没有嵌套在一个大函数中,而是完全分开放置在同一个文件中。

你尝试将第一个函数更改为以下方式:

export default function OrderCheckoutScreen({count})

然后在第一个函数中添加以下内容:

OrderCheckoutScreen({count})

但是你得到了未定义的计数值。

英文:

the first function is this and i want to use count inside of the second function:

const ProductListItem = ({
  item: {id, title, description, price, currency, images, category}, 
  imageSize,
  margins,
  onPress,
}) => {
    const [count, setCount]=useState(0);


    const countAddHandler = () => {
      setCount(count + 1)
    }
    const countSubtractHandler = () => {
      setCount(count - 1)
    }
    const countNumHandler= () => {
      if (count >= 0) {
        return count
      }
      else
      setCount(0)
      return count
    }

  return ( 
etc...

This is the second function:

export default function OrderCheckoutScreen({}) {
  const navigation = useNavigation();
  const dispatch = useDispatch();

  const {selected_products, loading, success, message} = useSelector(
    state => state.products.orderCheckout,
  ); 
etc...

Note that their is no big function that they are all nested in but these function are in the same file completely separated from each other.

i tried changing the first function to look like this:
export default function OrderCheckoutScreen({count})

while adding this to the first function:
OrderCheckoutScreen({count})

but i got count as undefined value.

答案1

得分: 1

Sure, here are the translated parts:

你需要在函数/组件之外创建你的useState Hook,然后在调用它们时传递,示例:

const [count, setCount] = useState(0)
...
return (
    <>
    <Component1 count={count} setCount={setCount} />
    <Component2 count={count} setCount={setCount} />
    </>
)

通过将count和setCount作为props传递,你可以在组件内部同时使用它们,它们将共享相同的值和相同的"setCount"函数。

然后,你只需要在组件的开头从props对象中提取这些值。

Component1.jsx

function Component1(props) {
    const {count, setCount} = props
    // 正常使用count和setCount
    return (
        你想要的内容
    )
};
export default Component1;

根据你的情况进行适应:

const ProductListItem = ({
  item: {id, title, description, price, currency, images, category}, 
  imageSize,
  margins,
  onPress,
  count,   // 在这里添加
  setCount // 在这里添加 
}) => {
    // const [count, setCount]=useState(0); <- 移除此行
etc...

以及

export default function OrderCheckoutScreen({count, setCount}) { // 在这里添加
  const navigation = useNavigation();
  const dispatch = useDispatch();

  const {selected_products, loading, success, message} = useSelector(
    state => state.products.orderCheckout,
  );

最后,将你的useState()移到调用这些组件的文件中。

const [count, setCount] = useState(0)
...
<ProductListItem ... count={count} setCount={setCount} />
<OrderCheckoutScreen ... count={count} setCount={setCount} />
英文:

You need to create your useState Hook outside the function/components and then pass when you call them, example:

const [count, setCount] = useState(0)
...
return (
    &lt;&gt;
    &lt;Component1 count=count setCount=setCount /&gt;
    &lt;Component2 count=count setCount=setCount /&gt;
    &lt;/&gt;
)

By passing count and setCount as props, you can use them both inside the components and they will share the same value and the same "setCount" function.

Then, all you need to do is in the start of the component pick those values from the props object.

Component1.jsx

function Component1(props) {
    const {count, setCount} = props
    // use count and setCount normally
    return (
        whatever you want
    )
};
export default Component1;

Adapting to your case:

const ProductListItem = ({
  item: {id, title, description, price, currency, images, category}, 
  imageSize,
  margins,
  onPress,
  count,   // Add here
  setCount // Add here 
}) =&gt; {
    // const [count, setCount]=useState(0); &lt;- Remove this line
etc...

and

export default function OrderCheckoutScreen({count, setCount}) { // Add here
  const navigation = useNavigation();
  const dispatch = useDispatch();

  const {selected_products, loading, success, message} = useSelector(
    state =&gt; state.products.orderCheckout,
  );

And finally move your useState() to the file where those components are called.

const [count, setCount] = useState(0)
...
    &lt;ProductListItem ... count=count setCount=setCount /&gt;
    &lt;OrderCheckoutScreen ... count=count setCount=setCount /&gt;
...

huangapple
  • 本文由 发表于 2023年7月3日 04:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定